問題をデバッグしているうちに、そのマングースのソートが正しく動作していないことがわかりました。コードを削除して簡単なテストをしました。あなたはそれが失敗することを見て実行することができます。マングースのソートが正しく動作しない
誰かが以前に見たことがありますか?たぶん私は何かを逃している?
ご協力いただきありがとうございます。
var mongoose = require('mongoose');
var assert = require('assert');
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost/test');
var CarSchema = new mongoose.Schema({
name: String
});
mongoose.model('Car', CarSchema);
var CarsSchema = new mongoose.Schema({
car: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Car'
},
quantity: Number
});
var OrderSchema = new mongoose.Schema({
suborders: [CarsSchema]
});
mongoose.model('Cars', OrderSchema);
var Car = mongoose.model('Car');
var Cars = mongoose.model('Cars');
Car.create([
{
name: 'Tesla'
},
{
name: 'BMW'
}
], function (err, objs) {
Cars.create({
suborders: [
{
car: objs[1]._id, //BMW
quantity: 1
}, {
car: objs[0]._id, //Tesla
quantity: 2
}
]
}, function (err, order) {
Cars.findById(order._id)
.populate({
path: 'suborders.car',
options: {
sort: '-name'
}
}).exec(function (err, cars) {
assert.equal(cars.suborders[0].car.name, 'Tesla', 'DESC: 0 should be Tesla');
assert.equal(cars.suborders[1].car.name, 'BMW', 'DESC: 1 should be BMW');
assert.equal(cars.suborders[0].quantity, 2, 'DESC: Tesla quantity should be 2');
assert.equal(cars.suborders[1].quantity, 1, 'DESC: BMW quantity should be 1');
});
});
});