mongoose: Query mit Sort
Wow, das ist eine eigenartige Syntax:
// Get notes by EntityIdentifier
app.get('/notes/all', function(req, res) {
docs = [];
Note.find({entity_id: req.query.entity_id}).sort('sticky','descending').each(function (err, doc) {
if (err) throw (err);
if(doc)
{
docs.push(doc);
}
else
{
res.send(docs.map(function(d) {
return d.toObject();
}));
}
});
});
Danke an ekryski.
UPDATE 17.05.2011:
Wusst ich doch, dass das irgendwie komisch ist… ;)
Hier eine optimierte Lösung:
app.get('/notes/all', function(req, res) {
Note.find({entity_id: req.query.entity_id}).sort(
'sticky',
'descending',
'created_at',
'descending').run(function (err, docs) {
if (err) throw (err);
res.send(docs.map(function(d) {
return d.toObject();
}));
});
});
Statt dem each() nun ein run().