mongoose - how to find data from mongodb collection in desending order? -


this question has answer here:

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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -