找到了解决方案!
技巧是在切片(
[]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。



