How to sort inner array in MongoDB?

MongoDBDatabaseBig Data Analytics

You can achieve this with the help of aggregate framework in MongoDB. To understand it, let us create a collection with document. The query to create a collection with document is as follows:

> db.sortInnerArrayDemo.insertOne(
...
...    {
...       "EmployeeDetails":
...       {
...          "EmployeeAddress":
...          {
...             "EmployeeCountry":
...             [
...                {
...                   "EmployeeZipCode":1003,
...                   "EmployeeStreetName":"7885 Trusel Street"
...                },
...                {
...                   "EmployeeZipCode":1001,
...                   "EmployeeStreetName":"7390 Gonzales Drive"
...                },
...                {
...                   "EmployeeZipCode":1002,
...                   "EmployeeStreetName":"444 N.Myres Rd."
...                }
...             ]
...          }
...       }
...    }
...
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6f07d3da34711ecf87a5b8")
}

Display all documents from a collection with the help of find() method. The query is as follows:

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

The following is the output:

{
   "_id" : ObjectId("5c6f07d3da34711ecf87a5b8"),
   "EmployeeDetails" : {
      "EmployeeAddress" : {
         "EmployeeCountry" : [
         {
            "EmployeeZipCode" : 1003,
            "EmployeeStreetName" : "7885 Trusel Street"
         },
         {
            "EmployeeZipCode" : 1001,
            "EmployeeStreetName" : "7390 Gonzales Drive"
         },
         {
            "EmployeeZipCode" : 1002,
            "EmployeeStreetName" : "444 N.Myres Rd."
         }
      ]}
   }
}

The following is the query to sort an inner array.

Case 1: Sort order in ascending order.

The query is as follows:

> db.sortInnerArrayDemo.aggregate(
... {$unwind: '$EmployeeDetails.EmployeeAddress.EmployeeCountry'},
... {$sort: {'EmployeeDetails.EmployeeAddress.EmployeeCountry.EmployeeZipCode': 1}},
... {$group: {_id: '$_id', 'EmpCountry': {$push:
'$EmployeeDetails.EmployeeAddress.EmployeeCountry'}}},
... {$project: {'EmployeeDetails.EmployeeAddress.EmployeeCountry':
'$EmpCountry'}}).pretty();

The following is the output displaying the inner array is sorted in ascending order on the basis of the EmployeeZipCode:

{
   "_id" : ObjectId("5c6f07d3da34711ecf87a5b8"),
   "EmployeeDetails" : {
      "EmployeeAddress" : {
         "EmployeeCountry" : [
            {
               "EmployeeZipCode" : 1001,
               "EmployeeStreetName" : "7390 Gonzales Drive"
            },
            {
               "EmployeeZipCode" : 1002,
               "EmployeeStreetName" : "444 N.Myres Rd."
            },
            {
               "EmployeeZipCode" : 1003,
               "EmployeeStreetName" : "7885 Trusel Street"
            }
         ]
      }
   }
}

Case 2: Sort order in descending order

The query is as follows:

> db.sortInnerArrayDemo.aggregate(
... {$unwind: '$EmployeeDetails.EmployeeAddress.EmployeeCountry'},
... {$sort: {'EmployeeDetails.EmployeeAddress.EmployeeCountry.EmployeeZipCode':-1}},
... {$group: {_id: '$_id', 'EmpCountry': {$push:
'$EmployeeDetails.EmployeeAddress.EmployeeCountry'}}},
... {$project: {'EmployeeDetails.EmployeeAddress.EmployeeCountry':
'$EmpCountry'}}).pretty();

The following is the output displaying the inner array is sorted in descending order on the basis of the EmployeeZipCode:

{
   "_id" : ObjectId("5c6f07d3da34711ecf87a5b8"),
   "EmployeeDetails" : {
      "EmployeeAddress" : {
         "EmployeeCountry" : [
            {
               "EmployeeZipCode" : 1003,
               "EmployeeStreetName" : "7885 Trusel Street"
            },
            {
               "EmployeeZipCode" : 1002,
               "EmployeeStreetName" : "444 N.Myres Rd."
            },
            {
               "EmployeeZipCode" : 1001,
               "EmployeeStreetName" : "7390 Gonzales Drive"
            }
         ]
      }
   }
}
raja
Published on 18-Mar-2019 12:38:15
Advertisements