 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 }Advertisements
 