【问题标题】:Mongoose searching FindOne with multiple argumentsMongoose 使用多个参数搜索 FindOne
【发布时间】:2013-09-23 22:32:52
【问题描述】:

我第一次尝试用 Angular + express + mongodb 构建一些东西,所以我可能会完全错误地处理这个问题。 Express 被用来提供 json。然后 Angular 会处理所有视图等。

我正在使用 Mongoose 与 Mongo 交互。

我有以下数据库架构:

var categorySchema = new mongoose.Schema({
  title: String, // this is the Category title
  retailers : [ 
    {
      title: String,  // this is the retailer title
      data: {       // this is the retailers Data
        strapLine: String,
        img: String ,  // this is the retailer's image
        intro: String,
        website: String,
        address: String,
        tel: String,
        email: String
      } 
    }
  ]
});

var Category = mongoose.model('Category', categorySchema);

在 Express 中,我有几条获取数据的途径:

 app.get('/data/categories', function(req, res) {
   // Find all Categories.
   Category.find(function(err, data) {
     if (err) return console.error(err);
     res.json(data)
   });
 });


 // return a list of retailers belonging to the category
 app.get('/data/retailer_list/:category', function(req, res) {
   //pass in the category param (the unique ID), and use that to do our retailer lookup
   Category.findOne({ _id: req.params.category }, function(err, data) {
     if (err) return console.error(err);
     res.json(data)
   }); 
 });

以上方法有效 - 我只是在尝试与单一零售商联系时遇到了大问题。我正在传递类别和零售商ID...我已经尝试了各种各样的事情-从对类别进行查找,然后对其中的内容进行findOne...但我就是无法让它工作。我可能会搞错这一切......

我在这里找到了这个帖子:findOne Subdocument in Mongoose 并实施了解决方案 - 但是,它返回了我所有的零售商 - 而不仅仅是我想要的那个。

// Returns a single retailer
app.get('/data/retailer_detail/:category/:id', function(req, res) {
  //pass in the category param (the unique ID), and use that to do our retailer lookup
 Category.findOne({_id: req.params.category , 'retailers.$': 1}, function(err, data) {
    console.log(data);
    if (err) return console.error(err);
    res.json(data)
  }); 
});    

谢谢, 抢

【问题讨论】:

  • 要使用投影运算符$,您还需要查询数组中的某些内容。 docs.mongodb.org/manual/reference/projection/positional/…
  • 您是如何将范围缩小到一家零售商的?
  • 感谢您的回复 :) 我通过 app.get 传入 :category - 所以: app.get('/data/retailer_detail/:category/:id', function(req , res) {.... 我正在尝试查找 - 找到类别文档,然后在其中搜索单个零售商
  • 您为什么没有将零售商的搜索作为查询的一部分添加到findOne
  • 呵呵 - 好点 :) '因为我' 真的不知道我在做什么。我已经更新了上面的最后一个代码块来做到这一点 - 但我仍然无法让它工作 - 它只是注销“null”

标签: node.js mongodb angularjs mongoose


【解决方案1】:

隔壁的人使用 Mongo + Express 并给了我一些指示:他们向我解释了 mongo 是如何工作的,并建议我应该使用 underscore.js 来协助我的过滤器。

他们说我需要提取整个类别 - 然后运行过滤器。我并不严格需要 , 'retailers._id' : req.params.id} 但他们说保留它,因为它保证只有当其中的项目包含该信息时才会返回该类别。我仍然不知道为什么或如何...所以不能真正将其标记为已解决.. 它已解决,但我还不明白为什么 - 所以会做更多阅读:)

app.get('/data/retailer_detail/:category/:id', function(req, res) {
  //pass in the category param (the unique ID), and use that to do our retailer lookup
 Category.findOne({_id: req.params.category , 'retailers._id' : req.params.id}, function(err, data) {
  var retailer = _.where(data.retailers , { id : req.params.id });
    if (err) return console.error(err);
    res.json(retailer)
  }); 
}); 

【讨论】:

    【解决方案2】:

    现在我看到了您的完整过滤器/查询,在这种情况下,您应该能够使用 array positional operator 作为投影的一部分,而不是进行客户端过滤:

    app.get('/data/retailer_detail/:category/:id', function(req, res) {
      //pass in the category param (the unique ID), and use that to do our retailer lookup
     Category.findOne({
        /* query */
        _id: req.params.category , 
        'retailers._id' : req.params.id
      },
      {  /* projection */
         "retailers.$" : 1 
      }, 
      function(err, data) {
      var retailer = _.where(data.retailers , { id : req.params.id });
        if (err) return console.error(err);
        res.json(retailer)
      }); 
    }); 
    

    要使{ "retailers.$" : 1 } 正常工作,查询必须包含来自数组元素的字段。 $ 运算符仅返回第一个匹配项。

    【讨论】:

    • 谢谢 - 这使得进一步操作变得更容易 - 将类别 ID 添加回零售商对象:) 谢谢
    猜你喜欢
    • 2016-08-19
    • 2021-12-04
    • 2015-08-28
    • 1970-01-01
    • 2022-09-26
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    相关资源
    最近更新 更多