mongoose - how to find data from mongodb collection in desending order? -
this question has answer here:
- in mongoose, how sort date? (node.js) 6 answers
suppose collection named books in mongodb , data follows.here ignore _id. please provide simple query finding books database costly book's document comes first.
>{ "name":"a", "cost":100 } { "name":"b", "cost":1000 } { "name":"f", "cost":400 } { "name":"e", "cost":400 } { "name":"z", "cost":800 }
my expected result be:
>{ "name":"b", "cost":1000 } { "name":"z", "cost":800 } { "name":"e", "cost":400 } { "name":"f", "cost":400 } { "name":"a", "cost":100 }
try
db.books.find({},{"name":1,_id:0,"cost":1}).sort({"cost":-1})
Comments
Post a Comment