NodeJS + Node-MongoDB-native: sort objects in alphabet order
Question
I'm trying to sort objects in collection by "name" parameter in alphabetical order. Here's the code I'm using:
app.get('/companies', loadUser, function(req,res){
db.collection("companies",function(err,collection){
collection.find({}, { sort:[['name',-1]]},function(err, companies) {
var companies_list = new Array();
companies.each(function(err, company){
...
});
})
});
I actually don't understand how exactly sort parameter works and what is -1. The official documentation didn't help me. Thanks!
Solution
Your code looks fine. I'd rather use
{ sort: [['name': 'desc']] }
because it's more explicit. Both methods work though.
When omitting the sort-direction:
{ sort: 'name'}
it sorts ascending.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow