[
{
_id: "5fd90412350de61e44657c8b"
description: 'Test1',
completed: true
},
{
_id: "5fd9040a350de61e44657c8a"
description: 'Test2',
completed: false
},
{
_id: "5f1d7d723bfe781024f734d9"
description: 'Test3',
completed: true
}
]
What I am trying to accomplish here, is to retrieve data from MongoDB filtering first by the documents that contain completed: true
, and the documents with completed: false
should appear last.
Is it possible?
db.Task.find()
logwriter
(Rodrigo Nascimento)
December 15, 2020, 9:22pm
2
Hey,
You could create an index for the “completed” field, and then run a find({ }).sort({ completed: -1}).
> db.test02.find( { } ).sort( { completed: -1} )
{ "_id" : ObjectId("5fd927ab63959199d5596799"), "description" : "Test1", "completed" : true }
{ "_id" : ObjectId("5fd927c463959199d559679d"), "description" : "Test5", "completed" : true }
{ "_id" : ObjectId("5fd927b363959199d559679a"), "description" : "Test2", "completed" : false }
{ "_id" : ObjectId("5fd927b963959199d559679b"), "description" : "Test3", "completed" : false }
{ "_id" : ObjectId("5fd927be63959199d559679c"), "description" : "Test4", "completed" : false }
>
3 Likes
It works perfectly for me. Thank you very much for your help!
2 Likes