简体   繁体   English

'$eq' 不适用于 mongodb 中的日期中的 arrays

[英]'$eq' is not working with arrays in dates in mongodb

'$eq' is not working with dates. '$eq' 不适用于日期。 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.我要搜索的文档上有一个 startdate 字段。 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.但是当我在'$eq'中给出日期数组时,它不起作用。 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.提前致谢。

I think you are trying to find where date is ISODate('2021-02-05') or ISODate('2021-02-12')我认为您正在尝试查找日期在哪里 ISODate('2021-02-05') 或 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')];将找到 startdate = [ISODate('2021-02-05'), ISODate('2021-02-12')];

To find where startdate equals at least one of your dates something like this:要查找 startdate 至少等于您的日期之一的位置,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM