通过Mongoose,Node.js,MongodDB中的特定属性查找嵌入的文档 [英] Finding an Embedded Document by a specific property in Mongoose, Node.js, MongodDB

查看:132
本文介绍了通过Mongoose,Node.js,MongodDB中的特定属性查找嵌入的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个应用程序,我使用的是Node.js,MongoDB,Mongoose&快递

For this app, I'm using Node.js, MongoDB, Mongoose & Express

所以我有一个Param对象,包含一个数据透视数组,我想从下面的概述读取枢轴上的某些数据

So I have a Param Object that contains an array of Pivots, and I want to read certain data from the pivots as outlined below

---in models.js-------------------------
    var Pivot = new Schema({
    value : String
  , destination : String
  , counter : Number
 });


var Param = new Schema({
    title : String
  , desc : String
  , pivots : [Pivot]
});


------------- in main.js --------------

var Param = db.model('Param');


app.get('/:title/:value', function(req, res){
    Param.findOne({"title":req.param('title')}, function(err, record){
           console.log(record.pivots);
           record.pivots.find({"value":req.param('value')}, function(err, m_pivot){
                    pivot.counter++;
                    res.redirect(m_pivot.destination);
           });
           record.save();
    });
});

我知道代码可以直到console.log(record.pivot),因为我有一个文档收集与正确的枢轴文件里面。

I know that the code works until console.log(record.pivots), since i got a doc collection with the right pivot documents inside.

但是,似乎没有一个find方法让我通过模式中定义的value属性来匹配嵌入式文档。是否可以使用.find()或.findOne()搜索嵌入式文档数组,如果没有,是否有一些简单的方法可以通过mongoose进行访问?

However, there does not seem to be a find method to let me match an embedded document by the 'value' property defined in the schema. Is it possible to search through this array of embedded documents using .find() or .findOne() , and if not, is there some easy way to access it through mongoose?

推荐答案

varunsrin,

varunsrin,

应该这样做

app.get('/:title/:value', function(req, res) {
  Param.findOne({'pivots.value': req.param('value'), "title":req.param('title')}},
                function(err, record) {
                  record.pivot.counter++;
                  res.redirect(m_pivot.destination);  
                 record.save();
               });
});

请注意查询的复合以匹配模式中的字段名称

Note the pluralization of the query to match the field name in your schema

这篇关于通过Mongoose,Node.js,MongodDB中的特定属性查找嵌入的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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