I am developing a web application using Sails.js and MongoDB. I have to query the database and sort the results by a complex function. Actually is not complex, but is more difficult than sort by a key. In this case, I have this collection
Users
{ user : "Luis", key1 : 23, key2 : 29 };
{ user : "Juan", key1 : 53, key2 : 22 };
{ user : "Paco", key1 : 26, key2 : 42 };
And I'm trying get the results order by key1 - key2
I have tried different queries having Mongo complex sorting? in account, but it didn't help me.
User.find({}).sort(function(doc1,doc2){return doc1.a . doc2.b })
.exec(function(err,users){console.log(users)});
I have checked that this way runs ok in the Robomongo console (MongoDB GUI client).
db.eval(function() {
return db.scratch.find().toArray().sort(function(doc1, doc2) {
return doc1.a - doc2.a
})
});
But I am not able to implement this using Sails and waterline orm. I would use native connection if necessary but I don't know how to do it.
Please help me, thanks you a lot!