简体   繁体   English

如何使用 mongodb C# 驱动程序获取子文档的集合?

[英]How to fetch collection of subdocument using mongodb C# driver?

I have following MongoDB document:我有以下 MongoDB 文档:

{
  "_id": {
    "$oid": "5fbfa0005c15aaf2eac69ba6"
  },
  "PostMedia": [
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img1.jpg",
      "Title": null,
      "Description": ""
    },
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img2.jpg",
      "Title": null,
      "Description": ""
    }
  ]
},
{
  "_id": {
    "$oid": "5fbfa0485c15aaf2eac69ba7"
  },
  "PostMedia": [
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img3.jpg",
      "Title": null,
      "Description": ""
    },
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img4.jpg",
      "Title": null,
      "Description": ""
    }
  ]
}

and I want to fetch all PostMedia to single array using MongoDB C# driver.我想使用 MongoDB C# 驱动程序将所有PostMedia获取到单个数组。 Here is the expected result:这是预期的结果:

[
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img1.jpg",
      "Title": null,
      "Description": ""
    },
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img2.jpg",
      "Title": null,
      "Description": ""
    }, {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img3.jpg",
      "Title": null,
      "Description": ""
    },
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img4.jpg",
      "Title": null,
      "Description": ""
    }
  ]

I had tried to use group aggregation function but it returned an array of array.我曾尝试使用组聚合 function 但它返回了一个数组数组。

在此处输入图像描述

Result I received:我收到的结果:

PostMedia:[{
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img1.jpg",
      "Title": null,
      "Description": ""
    },
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img2.jpg",
      "Title": null,
      "Description": ""
    }],[
     {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img3.jpg",
      "Title": null,
      "Description": ""
    },
    {
      "FilePath": "http://localhost:8886/localhost44323/image/",
      "Filename": "img4.jpg",
      "Title": null,
      "Description": ""
    }
  ]

C# code I have written so far is as follows:目前我写的C#代码如下:

  var group = new[]
                {
                new BsonDocument("$group",
                new BsonDocument
                {
                    { "_id", BsonNull.Value },
                    { "PostMedia", new BsonDocument("$push", "$PostMedia") }
                })
            };
                var result = collection.Aggregate<MediaList>(group).FirstOrDefault();
                return list;
            }

Is there any way to fetch subdocument by merging them is a single array.有没有办法通过合并它们来获取子文档是一个数组。

You have to $unwind before $group ,您必须在$group之前 $ $unwind

  • $unwind deconstruct PostMedia array $unwind解构PostMedia数组
  • $replaceRoot replace PostMedia object to root $replaceRootPostMedia object 替换为 root
  • $unset remove _id field $unset删除_id字段
  { $unwind: "$PostMedia" },
  {
    $group: {
      _id: null,
      PostMedia: { $push: "$PostMedia" }
    }
  },
  { $unset: "_id" } // remove field

Playground操场

C#: C#:

new BsonArray
{
    new BsonDocument("$unwind", "$PostMedia"),
    new BsonDocument("$group", 
    new BsonDocument
        {
            { "_id", BsonNull.Value }, 
            { "PostMedia", 
    new BsonDocument("$push", "$PostMedia") }
        })
    new BsonDocument("$unset", "_id")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM