In this tutorial, I will cover the $sort modifier in MongoDB. The $sort modifier will help us modify the order of the elements in an array when we conduct a $push operation.
To arrange in ascending order, we specify 1. For descending order, we specify, -1. The $sort modifier must be used together with the $each operator. Only the $sort modifier has an impact if you give an empty array [] to the $each operator.
This guide will give you several examples of using the $sort modifier in MongoDB. So, let us get started.
The $sort Modifier in MongoDB Syntax
{
$push: {
field: {
$each: [ value1, value2, ... ],
$sort: sort specification
}
}
}
The newer versions of the $sort modifier accept array elements that are not documents. However, the previous versions did not.
Using the $sort Modifier in MongoDB
Let us look at different examples to work with the $sort modifier in MongoDB.
Sorting an Array of Documents by a Field Using the $sort Modifier in Ascending Order
In this example, I will take a small database collection where we shall sort an array of documents by field in the ascending order inside the mongo shell using the $sort modifier in MongoDB.
- Start up the Mongo server
- Use the database you want to work in using the use db_name command
- Let us take a look at our animals collection in our animalShelter database:
> db.animals.find().pretty()
{
"_id" : ObjectId("6171984b68378cae00d83ac7"),
"animal" : "Cats",
"Maine Coons" : [
{
"name" : "Lucy",
"age" : 3
},
{
"name" : "Lola",
"age" : 2
},
{
"name" : "Lola",
"age" : 5
}
]
}
{
"_id" : ObjectId("6171984b68378cae00d83ac8"),
"animal" : "Dogs",
"Chihuahuas" : [
{
"name" : "Nancy",
"age" : 8
},
{
"name" : "Nora",
"age" : 10
},
{
"name" : "Norman",
"age" : 7
}
]
}
{
"_id" : ObjectId("6171984b68378cae00d83ac9"),
"animal" : "Hamsters",
"Dwarf Roborovski Hamsters" : [
{
"name" : "Henry",
"age" : 1
},
{
"name" : "Harry",
"age" : 2
},
{
"name" : "Hannah",
"age" : 4
}
]
}
- Now, let us push more hamsters into the Dwarf Roborovski Hamsters array. We also want to sort the elements in an ascending age field order:
> db.animals.update(
... { "animal" : "Hamsters" },
... {
... $push: {
... "Dwarf Roborovski Hamsters" : {
... $each: [ { "name" : "Harrison", age: 5 }, { "name" : "Harley", age: 0.5 }, { "name" : "Heidi", age: 3 } ],
... $sort: { age: 1 }
... }
... }
... }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
- Now, let us find all documents to check if the operation was successful:
> db.animals.find().pretty()
{
"_id" : ObjectId("6171984b68378cae00d83ac7"),
"animal" : "Cats",
"Maine Coons" : [
{
"name" : "Lucy",
"age" : 3
},
{
"name" : "Lola",
"age" : 2
},
{
"name" : "Lola",
"age" : 5
}
]
}
{
"_id" : ObjectId("6171984b68378cae00d83ac8"),
"animal" : "Dogs",
"Chihuahuas" : [
{
"name" : "Nancy",
"age" : 8
},
{
"name" : "Nora",
"age" : 10
},
{
"name" : "Norman",
"age" : 7
}
]
}
{
"_id" : ObjectId("6171984b68378cae00d83ac9"),
"animal" : "Hamsters",
"Dwarf Roborovski Hamsters" : [
{
"name" : "Harley",
"age" : 0.5
},
{
"name" : "Henry",
"age" : 1
},
{
"name" : "Harry",
"age" : 2
},
{
"name" : "Heidi",
"age" : 3
},
{
"name" : "Hannah",
"age" : 4
},
{
"name" : "Harrison",
"age" : 5
}
]
}
Great! Our hamsters are now systematically listed in ascending order by age with the help of the $sort modifier in MongoDB!
Sorting an Array of Documents by a Field Using the $sort Modifier
In this example, I will take a small database collection where we shall sort an array of documents by field inside the descending order inside the mongo shell.
> db.animals.update(
... { "animal" : "Dogs" },
... {
... $push: {
... "Chihuahuas" : {
... $each: [ { "name" : "Nathan", age: 3 }, { "name" : "Noah", age: 1 }, { "name" : "Nash", age: 0.2 } ],
... $sort: { age: -1 }
... }
... }
... }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
- Let us check our documents now:
> db.animals.find().pretty()
{
"_id" : ObjectId("6171984b68378cae00d83ac7"),
"animal" : "Cats",
"Maine Coons" : [
{
"name" : "Lucy",
"age" : 3
},
{
"name" : "Lola",
"age" : 2
},
{
"name" : "Lola",
"age" : 5
}
]
}
{
"_id" : ObjectId("6171984b68378cae00d83ac8"),
"animal" : "Dogs",
"Chihuahuas" : [
{
"name" : "Nora",
"age" : 10
},
{
"name" : "Nancy",
"age" : 8
},
{
"name" : "Norman",
"age" : 7
},
{
"name" : "Nathan",
"age" : 3
},
{
"name" : "Noah",
"age" : 1
},
{
"name" : "Nash",
"age" : 0.2
}
]
}
{
"_id" : ObjectId("6171984b68378cae00d83ac9"),
"animal" : "Hamsters",
"Dwarf Roborovski Hamsters" : [
{
"name" : "Harley",
"age" : 0.5
},
{
"name" : "Henry",
"age" : 1
},
{
"name" : "Harry",
"age" : 2
},
{
"name" : "Heidi",
"age" : 3
},
{
"name" : "Hannah",
"age" : 4
},
{
"name" : "Harrison",
"age" : 5
}
]
}
Awesome! Our dogs are now ordered in a descending fashion after the $sort modifier in MongoDB operation.
Read More: $eq Operator in MongoDB: Comprehensive 2021 Guide
Conclusion
We learned how to use the $sort modifier in MongoDB. We hope you enjoyed this tutorial. For more such easy tutorials, make sure you follow CodeForGeek.