Organizational Research By

Surprising Reserch Topic

finding an embedded document by a specific property in mongoose node js mongod


finding an embedded document by a specific property in mongoose node js mongod  using -'mongodb,node.js,express,mongoose'

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?
    

asked Sep 24, 2015 by mca.agarwal
0 votes
8 views



Related Hot Questions

4 Answers

0 votes

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

answered Sep 24, 2015 by amit_cmps
0 votes

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();
               });
});
answered Sep 24, 2015 by jekbishnoi
0 votes

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

for (var i=0; i 

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.

answered Sep 24, 2015 by vimaldas2005
0 votes

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 :/

answered Sep 24, 2015 by mcasudhir

...