Is there a MongoDB query to concatenate deep sub-lists?


Concatenate deep sub-lists using aggregate() along with $unwind. Let us create a collection with documents −

> db.demo70.insertOne(
...    {
...
...       "first" : [
...          {
...             "details" : {
...                "second" : [
...                   {
...                      "StudentDetails" : {
...                      "Score" : 10
...                   }
...                },
...                {
...                   "StudentDetails" : {
...                   "Score" : 20
...                }
...             },
...             {
...                "StudentDetails" : {
...                   "Score" : 30
...                }
...             }
...          ]
...       }
...    },
...    {
...       "details" : {
...          "second" : [
...             {
...                "StudentDetails" : {
...                "Score" : 11
...                }
...             },
...             {
...                "StudentDetails" : {
...                   "Score" : 18
...                }
...             },
...             {
...                "StudentDetails" : {
...                   "Score" : 29
...                   }
...                }
...             ]
...          }
...       }
...    ]
... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e29ad4d0912fae76b13d76d")
}

Display all documents from a collection with the help of find() method −

> db.demo70.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5e29ad4d0912fae76b13d76d"),
   "first" : [
      {
         "details" : {
            "second" : [
                  {
                     "StudentDetails" : {
                     "Score" : 10
                  }
               },
               {
                  "StudentDetails" : {
                     "Score" : 20
                  }
               },
               {
                  "StudentDetails" : {
                     "Score" : 30
                  }
               }
            ]
         }
      },
      {
         "details" : {
            "second" : [
               {
                  "StudentDetails" : {
                     "Score" : 11
               }
            },
            {
               "StudentDetails" : {
                  "Score" : 18
                  }
               },
               {
                  "StudentDetails" : {
                     "Score" : 29
                  }
               }
            ]
         }
      }
   ]
}

Following is the query to concatenate deep sub-lists −

> db.demo70.aggregate([
... { $unwind: "$first" },
... { $unwind: "$first.details.second" },
... { $sort: { "first.details.second.StudentDetails.Score": -1 } },
... { $limit: 3 },
... { $replaceRoot: { newRoot: "$first.details.second.StudentDetails" } },
... { $sort: { "Score": 1 } }
... ]);

This will produce the following output −

{ "Score" : 20 }
{ "Score" : 29 }
{ "Score" : 30 }

Updated on: 30-Mar-2020

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements