Насколько я знаю, можно сортировать заполненные документы с помощью Mongoose (source).
Я ищу способ сортировки запроса по одному или нескольким заполняемым полям.
Рассмотрим две схемы Mongoose:
var Wizard = new Schema({
name : { type: String }
, spells : { [{ type: Schema.ObjectId, ref: 'Spell' }] }
});
var Spell = new Schema({
name : { type: String }
, damages : { type: Number }
});
Пример JSON:
[{
name: 'Gandalf',
spells: [{
name: 'Fireball',
damages: 20
}]
}, {
name: 'Saruman',
spells: [{
name: 'Frozenball',
damages: 10
}]
}, {
name: 'Radagast',
spells: [{
name: 'Lightball',
damages: 15
}]
}]
Я хотел бы сортировать этих волшебников своими повреждениями заклинаний, используя что-то вроде:
WizardModel
.find({})
.populate('spells', myfields, myconditions, { sort: [['damages', 'asc']] })
// Should return in the right order: Saruman, Radagast, Gandalf
Я действительно делаю эти роли руками после запроса и хотел бы оптимизировать это.