Search code examples
node.jsmongodbexpressmongoosenode-mongodb-native

Findone for multiple queries not working mongoose


I am using mongoose with nodejs express framework

Here is the document in mongo :

{                                                                        
        "_id" : ObjectId("59c1fe16b68d844d8473c6fe"),                    
        "is_active" : "0",                                               
        "membership_expiry_date" : ISODate("2018-09-20T05:35:18.485Z"),  
        "date_of_joining" : ISODate("2017-09-20T05:35:18.485Z"),         
        "user_name" : "Max M",                                         
        "first_name" : "Max",                                          
        "password" : "MyPwd",                                            
        "address" : "NewTown",                                         
        "email_id" : "max@gmail.com",                                    
        "phone_number" : "12345678901",                                    
        "books_borrowed" : [                                             
                {                                                        
                        "due_date" : ISODate("2017-09-22T05:35:18.511Z"),
                        "book_id" : "1234",                              
                        "is_renewed" : true,                             
                        "_id" : ObjectId("59c1fe16b68d844d8473c700")     
                },                                                       
                {                                                        
                        "due_date" : ISODate("2017-09-22T05:35:18.531Z"),
                        "book_id" : "1235",                              
                        "is_renewed" : false,                            
                        "_id" : ObjectId("59c1fe16b68d844d8473c6ff")     
                }                                                        
        ],                                                               
        "__v" : 0                                                        
}       

Now when I try to access books_borrowed where user_name=Max and book_id=1234 I am using

model.findOne({"user_name":"Max M","book_id":1234}, {
        'books_borrowed': 1
    },
    function(err, res) {
        console.log("##Err:: " + JSON.stringify(err) + " response:: " + JSON.stringify(res));
        if (err) {
            return callback('Error while updating...');
        } else {
            return callback(null, res);
        }
    });

This returns error = null and response is also null.

If I replace book_id by 'books_borrowed.book_id' then I get

{
  "_id": "59c1fe16b68d844d8473c6fe",
  "books_borrowed": [
    {
      "due_date": "2017-09-22T05:35:18.511Z",
      "book_id": "1234",
      "is_renewed": true,
      "_id": "59c1fe16b68d844d8473c700"
    },
    {
      "due_date": "2017-09-22T05:35:18.531Z",
      "book_id": "1235",
      "is_renewed": false,
      "_id": "59c1fe16b68d844d8473c6ff"
    }
  ]
}

Which also contains data with book_id = 1235.

I just want object containing data with book_id=1234.

What am I missing here ? Also I don't want those object ids being inserted in my books_borrowed object.


Solution

  • Use this to filter the books_borrowed result

    model.findOne({
        "user_name":"Max M",
        "books_borrowed": {
            $elemMatch: {"book_id": "1234"}
        }
    }, 
    {
        "user_name": 1,
        // ... as needed 
        "books_borrowed.$": 1
    });