1

I have following MongoDB document:

{
  "_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. 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.

enter image description here

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:

  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.

1 Answer 1

1

You have to $unwind before $group,

  • $unwind deconstruct PostMedia array
  • $replaceRoot replace PostMedia object to root
  • $unset remove _id field
  { $unwind: "$PostMedia" },
  {
    $group: {
      _id: null,
      PostMedia: { $push: "$PostMedia" }
    }
  },
  { $unset: "_id" } // remove field

Playground

C#:

new BsonArray
{
    new BsonDocument("$unwind", "$PostMedia"),
    new BsonDocument("$group", 
    new BsonDocument
        {
            { "_id", BsonNull.Value }, 
            { "PostMedia", 
    new BsonDocument("$push", "$PostMedia") }
        })
    new BsonDocument("$unset", "_id")
}
Sign up to request clarification or add additional context in comments.

8 Comments

Is there a way to ignore _id
it requires to add $unset stage after $group stage, { $unset: "_id" }
{ $replaceRoot: { newRoot: "$PostMedia" } } is throwing me error what is wrong here can you guid me
what is the error and where is throwing error?. i don't know c# if the error is from c#, but will try.
can you go through this snippet mongoplayground.net/p/w_hoDgvWmcq
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.