Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在mongoosejs中搜索引用文档_Node.js_Mongodb_Mongoose - Fatal编程技术网

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);
});