如何使用官方的Mongo驱动程序在MongoDB中查找嵌套文档。

huangapple go评论77阅读模式
英文:

How to find nested documents mongodb using the official mongo driver

问题

我有这个文档,并希望对其进行过滤,以获取 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)
    }
}

请注意,我只是将代码部分翻译成了中文,其他部分保持原样。

英文:

I have this document and would like to filter it to get and document inside the Content array :

[
{
    "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"
                }
            ]
        }
    ],
}

]

how should i go about so as to filter this document i tried this but it didn't work :

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)
		}
	}

答案1

得分: 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


<details>
<summary>英文:</summary>


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][1]


  [1]: https://mongoplayground.net/p/gQyDOIuqtnF

</details>



# 答案2
**得分**: 0

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)
}

这段代码帮助我找到了嵌套的文档,感谢 @YuTing 的帮助。

<details>
<summary>英文:</summary>

    iuud, _ := primitive.ObjectIDFromHex(subsectionid)
    pipeline := []bson.M{
		{&quot;$match&quot;: bson.M{&quot;Name&quot;: name}},
		{&quot;$unwind&quot;: &quot;$Section&quot;},
		{&quot;$unwind&quot;: &quot;$Section.Content&quot;},
		{&quot;$match&quot;: bson.M{&quot;Section.Content.ID&quot;: iuud}},
		{&quot;$project&quot;: bson.M{
			&quot;Subsection_Title&quot;: &quot;$Section.Content.Subsection_Title&quot;,
			&quot;Content&quot;:       &quot;$Section.Content.Content&quot;,
            &quot;_id&quot;: &quot;$Section.Content.ID&quot;,
		}},
	} 
	iter, err := collection.Aggregate(ctx, pipeline)
	if err != nil {
		return nil, err
	}
	var results []bson.M
	if err = iter.All(context.TODO(), &amp;results); err != nil {
		log.Fatal(err)
	}

This helped me finding the nested documents Thanks @YuTing 

</details>



huangapple
  • 本文由 发表于 2022年1月30日 14:32:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/70912402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定