我正在尝试使用节点和猫鼬的填充方法来在查询中“加入”2 个集合。以下是我的架构设置:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ShopSchema = new Schema({
ssss: { type: Schema.Types.ObjectId, required :true, ref: 'Stat' },
ratings: [RatingSchema]
});
var RatingSchema = new Schema({
stat: { type: Schema.Types.ObjectId, required :true, ref: 'Stat' }
}, {_id: false});
我还设置了 Stat mongoose 模型,以便查询可以正常工作(但结果不是我所期望的)。
我尝试执行以下查询:
ShopSchema.statics.load = function(id, cb) {
this.findOne({
_id: id
}).populate('ssss', '_id stat_id').exec(cb);
};
mongoose.model('Shop', ShopSchema);
这给了我正确的结果,并且正确引用了 ssss。结果是这样的。
"ssss":{"_id":"5406839ad5c5d9c5d47091f0","stat_id":1}
但是,以下查询给了我错误的结果。
ShopSchema.statics.load = function(id, cb) {
this.findOne({
_id: id
}).populate('ratings.stat', '_id stat_id').exec(cb);
};
mongoose.model('Shop', ShopSchema);
这给了我所有结果的 rating.stat = null 。有人能告诉我我做错了什么吗?谢谢。