Finding an Embedded Document by a specific property in Mongoose, Node.js, MongodDB

后端 未结 5 1580
不知归路
不知归路 2021-01-12 21:30

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

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

相关标签:
5条回答
  • 2021-01-12 22:00

    varunsrin,

    This should do it

    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

    0 讨论(0)
  • 2021-01-12 22:07

    I think you are looking for the "$in" keyword?

    As in:

    {a: {$in: [10, "hello"]}}
    

    source: MongoDB Queries CheatSheet

    0 讨论(0)
  • 2021-01-12 22:11

    You can querying using embedded document properties like this:

    {'pivot.value': req.param('value')}}
    

    Update in response to comment:

    app.get('/:title/:value', function(req, res) {
      Param.findOne({'pivot.value': req.param('value'), "title":req.param('title')}},
                    function(err, record) {
                      record.pivot.counter++;
                      res.redirect(m_pivot.destination);  
                     record.save();
                   });
    });
    
    0 讨论(0)
  • 2021-01-12 22:13

    the biggest problem with this is that if your req has some fields empty (that should act as wildcard), you will not find anything since mongo tries to match empty params as well, so searching for {"user":"bob", "color":""} is not the same as {"user":"bob", "color":"red"} or {"user":"bob"}. this means that you have to first create a query object and filter out any unused parameters before you pass it in, and if you create a query object, you can no longer do something like "user.name=..." because mongo interperets this as an error since it does not first resolve the object literal into a string. Any ideas on this problem?

    ps. You'd think it would be easy enough to make an object like: user.name="bob"; user.color:"green"; user.signup.time="12342561" and then just use user as a query object :/

    0 讨论(0)
  • 2021-01-12 22:14

    I solved it temporarily using a simple for loop to parse the object array as follows:

    for (var i=0; i <record.pivots.length; i++){
       if (record.pivots[i].value == req.param('value')){
          res.redirect(record.pivots.destination);
       } 
    }
    

    However, I still think that Mongoose must have a simpler way of interacting with embedded documents - and this loop is somewhat slow, especially when the number of embedded documents grows large.

    If anyone has any suggestions for a faster way to search this object array either in js or with a mongoose function, please post below.

    0 讨论(0)
提交回复
热议问题