栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Go中的MongoDB聚合查找(mgo.v2)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Go中的MongoDB聚合查找(mgo.v2)

找到了解决方案!

技巧是在切片(

[]bson.M
)中创建查询并稍微更改查询的结构:

query := []bson.M{{  "$lookup": bson.M{ // lookup the documents table here    "from":         "documents",    "localField":   "_id",    "foreignField": "folderID",    "as":"documents",  }},  {"$match": bson.M{    "level": bson.M{"$lte": user.Level},    "userIDs": user.ID,}}}pipe := collection.Pipe(query)err := pipe.All(&folders)

我在mgo的Pipe函数文档中找到了线索。另外,我必须

documents
在我的
Folders
结构中更改该字段的标签,以便mgo对该字段进行标注:

type Folder struct {  ID        bson.ObjectId   `json:"id" bson:"_id"`  Name      string          `json:"name"`  Level     int  `json:"level"`  UserIDs   []bson.ObjectId `json:"userIDs" bson:"userIDs"`  Users     []User          `json:"-" bson:"-"` // doesn't get stored in the database  documents []document      // `json:"-" bson:"-" Removed this so that mgo can unmarshal      // the documents correctly}

现在,我只需要想出一种方法,即可

documents
在保存时不将字段存储在数据库中
Folder



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369556.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号