【发布时间】:2016-04-05 22:48:41
【问题描述】:
在调试问题时,我发现猫鼬排序无法正常工作。我删除了我们的代码并进行了简单的测试。你可以运行它看看它会失败。
也许有人以前见过?也许我错过了什么?
感谢您的帮助!
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');
});
});
});
【问题讨论】:
标签: javascript node.js mongodb mongoose