2

In mongodb there is a document like below,

{
   "_id": ObjectId("57443657ee5b5ccc30c4e6f8"),
   "name": "Kevin",
   "email": "[email protected]",
   "password": "$2a$13$iZ0PhuY6VlBF6qC9rUredrG39Fw5pEKkIh.VCLiGsLPZMKYveyzey",
   "mobile": "9980896745",
   "__v": NumberInt(0),
   "ocassionTypes": [
     {
       "occasiontype": "Anniversary",
       "date": "2016-05-30T18:30:00.000Z" 
    },
     {
       "occasiontype": "Donation",
       "date": "2016-07-24T18:30:00.000Z" 
    },
     {
       "occasiontype": "House Warming",
       "date": "2016-09-21T18:30:00.000Z" 
    } 
  ] 
}

So I have written a query in Nodejs to search occasiontype element in ocassionTypes array like below,

router.post('/find-registry', function(req, res){
    var uEmail = req.body.email;
    var uocType = req.body.userOccasion;
    var findUserId = function(db, callback) {
        
            var cursor =db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}}).toArray(function(err, docs1){
                if(err){  
                    callback(new Error("Some problem"));
                } else {
                    callback(null,docs1);
                } 
            });
    };
        
    MongoClient.connect(config.database, function(err, db) {
        assert.equal(null, err);
        findUserId(db, function(err,docs1) {
            db.close();
            if(err) return res.json({result:null})
            else
            return res.json({result1:docs1});
        });
    });                     
});

Using this query I am getting 0th index element, but if I give 1st and 2nd element it always shows only 0th index in the output.

In front end I have given input as shown in the picture below.

file.html

front end image

Is there any wrong in my query? please help me to fix this.

2 Answers 2

2

your query is right but it will give matched document with full array

just add projection in your query

db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}},{email:1, ocassionTypes: {$elemMatch: {occasiontype:uocType}}})
Sign up to request clarification or add additional context in comments.

6 Comments

Its worked now. But it not working when the ocassionTypes array contains only one element like { "_id": ObjectId("56fe44836ce2226431f5388f"), "name": "Hemanth", "email": "[email protected]", "password": "$2a$10$QeI.ntMGHF0jjAtWh/bBDu.RnZO7t5BUmVQJ.r7FxGdFM2.5.7erS", "__v": NumberInt(0), "mobile": "9989786745", "ocassionTypes": [ { "occasiontype": "Birthday", "date": "2016-04-17T18:30:00.0Z" } ] }
i check it ,it working me . when one element in array
Actually i have added one more condition to query. Now my query is as shown below var cursor =db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType, date:oDate}}}, {name: 1, ocassionTypes: {$elemMatch: {occasiontype: uocType, date:oDate}}}).toArray(function(err, docs1){ if(err){ callback(new Error("Some problem")); } else { callback(null,docs1); } });
i think i dint see any extra condition in yours , pls mention ur extra condtion
ocassionTypes: {$elemMatch: {occasiontype:uocType, date:oDate}}}, {name: 1, ocassionTypes: {$elemMatch: {occasiontype: uocType, date:oDate}}}
|
2

If you are searching in sub document, mongodb returns all sub-document instead of matched sub-document. You can limit no. of sub-document using following code.

var cursor =db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}},{email: 1, ocassionTypes: {$elemMatch: {occasiontype: uocType}}}).toArray(function(err, docs1){
                if(err){  
                    callback(new Error("Some problem"));
                } else {
                    callback(null,docs1);
                } 
            }); 

It is not tested but it should work.

1 Comment

Its worked now. But it not working when the ocassionTypes array contains only one element like { "_id": ObjectId("56fe44836ce2226431f5388f"), "name": "Hemanth", "email": "[email protected]", "password": "$2a$10$QeI.ntMGHF0jjAtWh/bBDu.RnZO7t5BUmVQJ.r7FxGdFM2.5.7erS", "__v": NumberInt(0), "mobile": "9989786745", "ocassionTypes": [ { "occasiontype": "Birthday", "date": "2016-04-17T18:30:00.0Z" } ] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.