【问题标题】:how to search for referenced document in mongoosejs如何在猫鼬js中搜索参考文档
【发布时间】:2015-02-23 16:32:41
【问题描述】:

我有两个架构 Organization 和 Location。在 Organization 架构上,我引用了位置架构:

Organization = new Schema({
  location: {
    type: Schema.ObjectId,
    ref: 'Location'
  } 
});

Location 架构包含国家、州、城市、地区等字段。

现在我将如何搜索带有特定国家或州或城市的Organization。

我在猫鼬文档中找到的是这样的:

Organization
.find()
.populate([{path:'location',select:'country state city district' },
 {/*other population paths*/}] /*Some constriants here*/).exec(function(err, orgs){
   console.log(orgs);
});

我如何实现这种类型的搜索。是否有任何插件可用于此类参考文档搜索?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您可以通过匹配查询来选择特定位置。请阅读文档query_populate。

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

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2017-05-20
      相关资源
      最近更新 更多