亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用官方 mongo 驅動程序查找嵌套文檔 mongodb

如何使用官方 mongo 驅動程序查找嵌套文檔 mongodb

Go
Helenr 2022-10-31 16:55:53
我有這個文檔,想過濾它以獲取并記錄在 Content 數組中:[{    "ID": "61f1244daeaea5f165851fc9",    "name": "Mulandi",    "author": "Owayo",    "description": "dnjsfnvlksfnvls",    "created_at": "2022-01-26T10:37:01.558Z",    "Section": [        {            "ID": "61f557213fd9b086c3a422c5",            "Title": "Weee",            "Content": [                {                    "ID": "61f5586e3fd9b086c3a422dc",                    "Subsection_Title": "Idk",                    "Content": "Something"                }            ]        }    ],}]我應該怎么做才能過濾這個文件我試過這個但它沒有用:pipeline := []bson.M{        {"$match": bson.M{"Name": name}},        {"$unwind": "$Section"},        {"$unwind": "$Section.Content"},        {"$project": bson.M{            "Section.Title":                sectiontitle,            "Section.Content.subsectionid": iuud,        }},    }    iter, err := collection.Aggregate(ctx, pipeline)    if err != nil {        return nil, err    }    var elem models.Course    for iter.Next(ctx) {        var elem models.Section        err = iter.Decode(&elem)        if err != nil {            log.Fatal(err)        }    }
查看完整描述

2 回答

?
DIEA

TA貢獻1820條經驗 獲得超2個贊

db.collection.aggregate([

  {

    "$match": {

      "name": "Mulandi"

    }

  },

  {

    "$unwind": "$Section"

  },

  {

    "$unwind": "$Section.Content"

  },

  {

    "$match": {

      "Section.Content.ID": "61f5586e3fd9b086c3a422dc"

    }

  },

  {

    "$project": {

      "Subsection_Title": "$Section.Content.Subsection_Title",

      "Content": "$Section.Content.Content",

      "_id": "$Section.Content.ID"

    }

  }

])

mongoplayground


查看完整回答
反對 回復 2022-10-31
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

iuud, _ := primitive.ObjectIDFromHex(subsectionid)

pipeline := []bson.M{

    {"$match": bson.M{"Name": name}},

    {"$unwind": "$Section"},

    {"$unwind": "$Section.Content"},

    {"$match": bson.M{"Section.Content.ID": iuud}},

    {"$project": bson.M{

        "Subsection_Title": "$Section.Content.Subsection_Title",

        "Content":       "$Section.Content.Content",

        "_id": "$Section.Content.ID",

    }},

iter, err := collection.Aggregate(ctx, pipeline)

if err != nil {

    return nil, err

}

var results []bson.M

if err = iter.All(context.TODO(), &results); err != nil {

    log.Fatal(err)

}


查看完整回答
反對 回復 2022-10-31
  • 2 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號