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

12,972

Solution 1

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

Solution 2

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();
               });
});
Share:
12,972
varunsrin
Author by

varunsrin

Updated on June 17, 2022

Comments

  • varunsrin
    varunsrin almost 2 years

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

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

    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 about 13 years
    I don't think so - $in lets you search within a collection where the value of a is 10 or hello. That is not what I am looking for here - for me, the basic query itself does not work, for some odd reason.
  • varunsrin
    varunsrin almost 13 years
    doesn't seem to work, could you show me where exactly i should be calling this in the code i outlined in the OP
  • varunsrin
    varunsrin almost 13 years
    In your suggestion, in the function record is a Param that contains an array of pivots. I still have to sort through the pivots again to find the right one. I want to be able to get a pointer to the right pivot so that I can go to the URL.
  • varunsrin
    varunsrin almost 13 years
    also, your function is wrong in terms of the variables used - "m_pivot" was my variable for the inner loop which you do not use. you reference the pivot as record.pivot & m_pivot. I do not understand the structure you have used - record.pivot does not exist - the query returns record with record.pivots an array of ALL the pivots, which I was getting with my code anyway. I want to return just the pivot I need with pivot.value = value
  • donald
    donald over 12 years
    @varunsrin this is not working for me. Did you make it work? thanks