Node.js 如何在mongoosejs中搜索引用文档
我有两个模式Node.js 如何在mongoosejs中搜索引用文档,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有两个模式组织和位置。在组织模式中,我引用了位置模式: Organization = new Schema({ location: { type: Schema.ObjectId, ref: 'Location' } }); Locationschema包含国家、州、城市、地区等字段 现在,我将如何搜索具有特定国家或州或城市的组织 我在Mongoose文档中发现的是: Organization .find() .populate([{path:'location',
组织
和位置
。在组织
模式中,我引用了位置模式:
Organization = new Schema({
location: {
type: Schema.ObjectId,
ref: 'Location'
}
});
Location
schema包含国家、州、城市、地区等字段
现在,我将如何搜索具有特定国家或州或城市的组织
我在Mongoose文档中发现的是:
Organization
.find()
.populate([{path:'location',select:'country state city district' },
{/*other population paths*/}] /*Some constriants here*/).exec(function(err, orgs){
console.log(orgs);
});
如何实现这种类型的搜索。是否有任何插件可用于此类型的参考文档搜索?您可以通过匹配查询来选择特定位置。请阅读文件
Organization
.find()
.populate([{
path:'location',
match : {state : req.query.state} //your query goes here
select:'country state city district'
}]).exec(function(err, orgs){
console.log(orgs);
});