One of my collections is a list of operations (or tasks) with some status. For example, a list of documents can look like this
{
_id: '57befe57fc956d2e3252890c',
task: 'Go buy milk',
status: 'pending'
},
{
_id: '57befe5efc956d2e3252890d',
task: 'Cook dinner',
status: 'complete'
},
{
_id: '57befe5efc956d2e3252890e',
task: 'Play games',
status: 'new'
}
I want to sort this list based on a their status, where new > pending > complete
.
How can I do that with MongoDB without having to create an extra field? I am asking as in my case the sorting order may be pre-configured (i.e. users could have their preferences to pending > new > complete
for example)
To sort documents in MongoDB, you need to use sort() method. The method accepts a document containing a list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending order while -1 is used for descending order.
This operation sorts the documents in the users collection, in descending order according by the age field and then in ascending order according to the value in the posts field.
MongoDB can perform sort operations on a single-field index in ascending or descending order. In compound indexes, the sort order determines whether the index can be sorted.
This means the only way to sort case insensitive currently is to actually create a specific "lower cased" field, copying the value (lower cased of course) of the sort field in question and sorting on that instead.
Based on what @chirdam said here is what the implementation looks like. Also the new sorted field is not present in the result as requested.
DataSet :
{
_id: '57befe57fc956d2e3252890c',
task: 'Go buy milk',
status: 'pending'
},
{
_id: '57befe5efc956d2e3252890d',
task: 'Cook dinner',
status: 'complete'
},
{
_id: '57befe5efc956d2e3252890e',
task: 'Play games',
status: 'new'
}
Query :
db.task.aggregate([
{ "$project" : {
"_id" : 1,
"task" : 1,
"status" : 1,
"order" : {
"$cond" : {
if : { "$eq" : ["$status", "new"] }, then : 1,
else : { "$cond" : {
"if" : { "$eq" : ["$status", "pending"] }, then : 2,
else : 3
}
}
}
}
} },
{"$sort" : {"order" : 1} },
{ "$project" : { "_id" : 1, "task" : 1, "status" : 1 } }
])
Output:
{ "_id" : "57befe5efc956d2e3252890e", "task" : "Play games", "status" : "new" }
{ "_id" : "57befe57fc956d2e3252890c", "task" : "Go buy milk", "status" : "pending" }
{ "_id" : "57befe5efc956d2e3252890d", "task" : "Cook dinner", "status" : "complete" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With