我在 Mongo 集合中有以下对象:
{
"active" : true,
"startDate": Date( 1434355148265 ), // Mon Jun 15 2015 15:59:08 GMT+0800 (HKT)
"endDate": null,
"lastFeedSearch": null,
"lastTopicSearch": null
}
然后我尝试运行以下查询,但它似乎没有返回任何对象
{
"active":true,
"$and":[
{
"$or":[
{
"startDate":{
"$exists": false
}
},
{
"startDate":{
"$lte": "2015-06-16T07:07:30+00:00"
}
}
]
},
{
"$or":[
{
"endDate":{
"$exists": false
}
},
{
"endDate":{
"$gte": "2015-06-16T07:07:30+00:00"
}
}
]
},
{
"$or":[
{
"$or":[
{
"lastTopicSearch":{
"$lte": "2015-06-16T06:07:30+00:00"
}
},
{
"lastTopicSearch":{
"$exists": false
}
}
]
},
{
"$or":[
{
"lastFeedSearch":{
"$lte": "2015-06-16T06:52:30+00:00"
}
},
{
"lastFeedSearch":{
"$exists": false
}
}
]
}
]
}
]
}
据我所知,所有这些 $or
条件至少应该匹配。
据我所知,这是因为 $exists
似乎没有考虑 null
值,尽管文档说它应该考虑。
最佳答案
来自 MongoDB official $exists
docs :
When <boolean> is true, $exists matches the documents that contain the field, including documents where the field value is null. If <boolean> is false, the query returns only the documents that do not contain the field.
换句话说,$exists
将 null
视为有效值。因此,{"$exists": false}
仅匹配相应字段为 undefined
的文档,而不是 null
。
尝试将您的字段与 null
匹配:
{
"lastTopicSearch": null
}
此查询将匹配 null
和 undefined
值。
关于mongodb - Mongo 查询 : $exist NULL value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30862887/