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

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

How to find nested documents mongodb using the official mongo driver

问题

我有这个文档,并希望对其进行过滤,以获取 Content 数组内的文档:

  1. [
  2. {
  3. "ID": "61f1244daeaea5f165851fc9",
  4. "name": "Mulandi",
  5. "author": "Owayo",
  6. "description": "dnjsfnvlksfnvls",
  7. "created_at": "2022-01-26T10:37:01.558Z",
  8. "Section": [
  9. {
  10. "ID": "61f557213fd9b086c3a422c5",
  11. "Title": "Weee",
  12. "Content": [
  13. {
  14. "ID": "61f5586e3fd9b086c3a422dc",
  15. "Subsection_Title": "Idk",
  16. "Content": "Something"
  17. }
  18. ]
  19. }
  20. ]
  21. }
  22. ]

我应该如何过滤这个文档?我尝试了以下方法,但没有成功:

  1. pipeline := []bson.M{
  2. {"$match": bson.M{"Name": name}},
  3. {"$unwind": "$Section"},
  4. {"$unwind": "$Section.Content"},
  5. {"$project": bson.M{
  6. "Section.Title": sectiontitle,
  7. "Section.Content.subsectionid": iuud,
  8. }},
  9. }
  10. iter, err := collection.Aggregate(ctx, pipeline)
  11. if err != nil {
  12. return nil, err
  13. }
  14. var elem models.Course
  15. for iter.Next(ctx) {
  16. var elem models.Section
  17. err = iter.Decode(&elem)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. }

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

英文:

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

  1. [
  2. {
  3. "ID": "61f1244daeaea5f165851fc9",
  4. "name": "Mulandi",
  5. "author": "Owayo",
  6. "description": "dnjsfnvlksfnvls",
  7. "created_at": "2022-01-26T10:37:01.558Z",
  8. "Section": [
  9. {
  10. "ID": "61f557213fd9b086c3a422c5",
  11. "Title": "Weee",
  12. "Content": [
  13. {
  14. "ID": "61f5586e3fd9b086c3a422dc",
  15. "Subsection_Title": "Idk",
  16. "Content": "Something"
  17. }
  18. ]
  19. }
  20. ],
  21. }

]

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

  1. pipeline := []bson.M{
  2. {"$match": bson.M{"Name": name}},
  3. {"$unwind": "$Section"},
  4. {"$unwind": "$Section.Content"},
  5. {"$project": bson.M{
  6. "Section.Title": sectiontitle,
  7. "Section.Content.subsectionid": iuud,
  8. }},
  9. }
  10. iter, err := collection.Aggregate(ctx, pipeline)
  11. if err != nil {
  12. return nil, err
  13. }
  14. var elem models.Course
  15. for iter.Next(ctx) {
  16. var elem models.Section
  17. err = iter.Decode(&elem)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. }

答案1

得分: 2

  1. db.collection.aggregate([
  2. {
  3. "$match": {
  4. "name": "Mulandi"
  5. }
  6. },
  7. {
  8. "$unwind": "$Section"
  9. },
  10. {
  11. "$unwind": "$Section.Content"
  12. },
  13. {
  14. "$match": {
  15. "Section.Content.ID": "61f5586e3fd9b086c3a422dc"
  16. }
  17. },
  18. {
  19. "$project": {
  20. "Subsection_Title": "$Section.Content.Subsection_Title",
  21. "Content": "$Section.Content.Content",
  22. "_id": "$Section.Content.ID"
  23. }
  24. }
  25. ])

mongoplayground

  1. <details>
  2. <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"
}
}
])

  1. [mongoplayground][1]
  2. [1]: https://mongoplayground.net/p/gQyDOIuqtnF
  3. </details>
  4. # 答案2
  5. **得分**: 0
  6. iuud, _ := primitive.ObjectIDFromHex(subsectionid)
  7. pipeline := []bson.M{
  8. {"$match": bson.M{"Name": name}},
  9. {"$unwind": "$Section"},
  10. {"$unwind": "$Section.Content"},
  11. {"$match": bson.M{"Section.Content.ID": iuud}},
  12. {"$project": bson.M{
  13. "Subsection_Title": "$Section.Content.Subsection_Title",
  14. "Content": "$Section.Content.Content",
  15. "_id": "$Section.Content.ID",
  16. }},
  17. }
  18. iter, err := collection.Aggregate(ctx, pipeline)
  19. if err != nil {
  20. return nil, err
  21. }
  22. var results []bson.M
  23. if err = iter.All(context.TODO(), &results); err != nil {
  24. log.Fatal(err)
  25. }
  26. 这段代码帮助我找到了嵌套的文档,感谢 @YuTing 的帮助。
  27. <details>
  28. <summary>英文:</summary>
  29. iuud, _ := primitive.ObjectIDFromHex(subsectionid)
  30. pipeline := []bson.M{
  31. {&quot;$match&quot;: bson.M{&quot;Name&quot;: name}},
  32. {&quot;$unwind&quot;: &quot;$Section&quot;},
  33. {&quot;$unwind&quot;: &quot;$Section.Content&quot;},
  34. {&quot;$match&quot;: bson.M{&quot;Section.Content.ID&quot;: iuud}},
  35. {&quot;$project&quot;: bson.M{
  36. &quot;Subsection_Title&quot;: &quot;$Section.Content.Subsection_Title&quot;,
  37. &quot;Content&quot;: &quot;$Section.Content.Content&quot;,
  38. &quot;_id&quot;: &quot;$Section.Content.ID&quot;,
  39. }},
  40. }
  41. iter, err := collection.Aggregate(ctx, pipeline)
  42. if err != nil {
  43. return nil, err
  44. }
  45. var results []bson.M
  46. if err = iter.All(context.TODO(), &amp;results); err != nil {
  47. log.Fatal(err)
  48. }
  49. This helped me finding the nested documents Thanks @YuTing
  50. </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:

确定