Yash
Yash

Reputation: 308

'$eq' is not working with arrays in dates in mongodb

'$eq' is not working with dates. Can Anyone help me on this.
So what I'm trying to do is I want to search documents based on multiple dates. There is a startdate field on the document that I want to search against. If I give a plain query, it will retrive the doc. But when I will give the array of dates in '$eq', it is not working. Can Someone point out the mistake or reformat this query to work.

db.getCollection('campaigngrid').find({
    startdate: { '$eq': [ ISODate('2021-02-05'), ISODate('2021-02-12') ] },
//    startdate: ISODate('2021-02-05'),
    active: true
})

So I've double check the query. And the document is also there. When I just search with simple query, it retrieves the docs(which I just commented). Thanks in Advance.

Upvotes: 0

Views: 147

Answers (1)

Aref Akbari
Aref Akbari

Reputation: 36

I think you are trying to find where date is ISODate('2021-02-05') or ISODate('2021-02-12')

But your problem is

 $eq: [ISODate('2021-02-05'), ISODate('2021-02-12')]

will find where startdate = [ISODate('2021-02-05'), ISODate('2021-02-12')];

To find where startdate equals at least one of your dates something like this:

db.getCollection('campaigngrid').find({
    startdate: { '$in': [ ISODate('2021-02-05'), ISODate('2021-02-12') ] },
    active: true
})

Upvotes: 1

Related Questions