Javascript 关于填充字段的查询
目前,我有两种模式:Javascript 关于填充字段的查询,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,目前,我有两种模式: var person = new schema({ id: Number, name: String, city: { type: schema.Types.ObjectId, ref: 'cities' } }); 及 我有两个模型: var persons = mongoose.model('persons', person); var cities = mongoose.model('cities', city); 一切正常。我可以在城市现场选择一
var person = new schema({
id: Number,
name: String,
city: {
type: schema.Types.ObjectId,
ref: 'cities'
}
});
及
我有两个模型:
var persons = mongoose.model('persons', person);
var cities = mongoose.model('cities', city);
一切正常。我可以在城市
现场选择一个人并使其返回城市
对象,完美
问题:
我想选择所有城市中有国家/地区缩写“NL”的人。我试过这个:
persons.find({
'city.country_short': 'NL'
}).populate('city').exec(function(err, data) {
//Data SHOULD obtain all persons living in a city with country_short "NL".
});
这不管用。我找到了一个有效的解决方案,但它是嵌套的。。我不喜欢它。它选择所有国家/地区缩写为“NL”的城市并绘制地图。然后在该查找(嵌套…)中,我查找所有人员并在映射数组中检查city$
请帮助我使用非嵌套解决方案。或者是另一种方法
谢谢 要将查询条件包含在上引用的架构中,请使用以下方法:
persons.find(...)
.populate({
path: 'city',
match: { country_short: 'NL'},
select: 'name -_id', /* limit fields to return */
options: { limit: 5 } /* limit documents to return */
})
.exec(callback);
谢谢你的回复。这是行不通的。我仍然得到所有的人,那些没有NL返回“city:null”的城市的人。
persons.find(...)
.populate({
path: 'city',
match: { country_short: 'NL'},
select: 'name -_id', /* limit fields to return */
options: { limit: 5 } /* limit documents to return */
})
.exec(callback);