猫鼬用多个参数搜索FindOne [英] Mongoose searching FindOne with multiple arguments

查看:102
本文介绍了猫鼬用多个参数搜索FindOne的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用Angular + express + mongodb构建对象,因此我可能完全以错误的方式进行操作. Express用于提供json.然后,Angular会处理所有视图等.

My first attempt at building something with Angular + express + mongodb, so I'm probably going about this completely the wrong way. Express is being used to serve up json. Angular then takes care of all the views etc.

我正在使用Mongoose与Mongo进行交互.

I'm using Mongoose to interact with Mongo.

我具有以下数据库架构:

I have the following database schema:

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中,我有两条路线来获取数据:

and in Express I have a couple of routes to get the data:

 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,但是我无法使其正常工作.我可能正在解决所有这些错误...

The above works - I'm just having big problems trying to get at a single retailer. I'm passing the category, and retailer id through... I've tried all sorts of things - from doing a find on the category, then a findOne on the contents within... but I just cant get it to work. I'm probably going about this all wrong...

我在以下位置找到了此线程: Mongoose中的findd子文档并实施了解决方案-但是,它返回了我所有的零售商-而不仅仅是我想要的那个零售商.

I found this thread here: findOne Subdocument in Mongoose and implemented the solution - however, it returns all my retailers - and not just the one I want.

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

谢谢, 罗布

推荐答案

现在,我看到了完整的过滤器/查询,您应该可以使用

Now that I see your full filter/query, you should be able to use the array positional operator in this case as part of the projection rather than doing client side filtering:

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 }正常工作,查询必须包括数组中元素的字段. $运算符仅返回第一个匹配项.

For the { "retailers.$" : 1 } to work properly, the query must include a field from an element in the array. The $ operator returns the first match only.

这篇关于猫鼬用多个参数搜索FindOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆