1

I have below collection in the DB, I want to retrieve data where birth month equal to given 2 months. lets say [1,2], or [4,5]

{   
    "_id" : ObjectId("55aa1e526fea82e9a4188f38"),
    "name" : "Nilmini",   
    "birthDate" : 6,
    "birthMonth" : 1
},
{   
    "_id" : ObjectId("55aa1e526fea82e9a4188f39"),
    "name" : "Ruwan",   
    "birthDate" : 6,
    "birthMonth" : 1
},{ 
    "_id" : ObjectId("55aa1e526fea82e9a4188f40"),
    "name" : "Malith",   
    "birthDate" : 6,
    "birthMonth" : 1
},
{   
    "_id" : ObjectId("55aa1e526fea82e9a4188f7569"),
    "name" : "Pradeep",   
    "birthDate" : 6,
    "birthMonth" : 7
}

I use below query to get the result set, I could get the result for give one month,now I want to get results for multiple months.

      var currentDay = moment().date();
        var currentMonths = [];
        var currentMonth = moment().month();
        if(currentDay > 20){
            currentMonths.push(moment().month());
            currentMonths.push(moment().month()+1);
        }else{
          currentMonths.push(currentMonth);
        }
    // In blow query I am trying to pass the array to the 'birthMonth', 
I'm getting nothing when I pass array to the query, I think there should be another way to do this,
    Employee.find(
            {
                "birthDate": {$gte:currentDay}, "birthMonth": currentMonths
            }, function(err, birthDays) {
            res.json(birthDays);
        });

I would really appreciate if you could help me to figure this out

1
  • Yes I had, but wrong way.Thank you... It is working now Commented Jul 26, 2015 at 4:18

1 Answer 1

3

You can use the $in operator to match against multiple values in an array like currentMonths.

So your query would be:

Employee.find(
    {
        "birthDate": {$gte:currentDay}, "birthMonth": {$in: currentMonths}
    }, function(err, birthDays) {
    res.json(birthDays);
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank you I fixed this.. using your previous comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.