Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to sort inner array in MongoDB?
To sort inner arrays in MongoDB, use the aggregation framework with $unwind, $sort, $group, and $project stages. This approach deconstructs the array, sorts the elements, and reconstructs the sorted array.
Sample Data
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 the document to see the original unsorted array ?
db.sortInnerArrayDemo.find().pretty();
{
"_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."
}
]
}
}
}
Case 1: Sort in Ascending Order
Sort the inner array by EmployeeZipCode in ascending order (1 = ascending) ?
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();
{
"_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 in Descending Order
Sort the inner array by EmployeeZipCode in descending order (-1 = descending) ?
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();
{
"_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"
}
]
}
}
}
How It Works
-
$unwind? Deconstructs the array into separate documents -
$sort? Sorts the unwound documents by the specified field -
$group? Groups documents back by _id and pushes sorted elements into a new array -
$project? Shapes the output to match the original document structure
Conclusion
Use MongoDB's aggregation pipeline to sort inner arrays by unwinding, sorting, grouping, and projecting. The $sort stage accepts 1 for ascending and -1 for descending order on any field within the array elements.
Advertisements
