简体   繁体   English

mongodb complex或/和查询

[英]mongodb complex or/and query

So I have a collections which stores the start and end times for an event and I would like a user to be able to query for events by specifying "filter" start and end times. 因此,我有一个集合来存储事件的开始和结束时间,我希望用户能够通过指定“过滤器”开始和结束时间来查询事件。 The query should return any events that intersect with the filter times. 查询应返回与过滤时间相交的所有事件。 I thought I was building the query correctly, but it's not returning the results I want. 我以为我正确地建立了查询,但是没有返回我想要的结果。

Example data (simplified mongo document to get the idea, times are HHMMSS): 示例数据(获得想法的简化mongo文档,时间为HHMMSS):

{ ..., "start" : "050520", "end" : "051309" } //Should match
{ ..., "start" : "051125", "end" : "051925" } //Should match
{ ..., "start" : "052049", "end" : "052420" } //Should match
{ ..., "start" : "050000", "end" : "050500" }
{ ..., "start" : "052100", "end" : "052721" }

The query I tried was (Forgive me if I missed a bracket here, that's not the issue): 我尝试的查询是(请原谅我,如果我在这里错过了括号,那不是问题):

find( { $or : [ 
            { start : { $gte : "050600", $lt : "052100" } }, 
            { end : { $lt : "052100", $gt : "050600" } } 
        ] } )

Which only returned one of the three results. 其中仅返回三个结果之一。 Switching the order of the $or parts ( querying against the end times before the start times) gives me two results but not all three. 切换$或部件的顺序(在开始时间之前查询结束时间)会得到两个结果,但不是全部三个。 I seem to remember reading that for compound queries mongo throws away results that don't match the first part, before applying the second part of the query, but I thought the $or operator would handle that. 我似乎记得读过,对于复合查询,mongo在应用查询的第二部分之前会丢弃与第一部分不匹配的结果,但是我认为$ or运算符会处理该问题。 Any ideas? 有任何想法吗? Is my query incorrect, is this a limitation of querying in mongo? 我的查询不正确吗,这是对mongo中查询的限制吗?

Using MongoDB 2.4.5 I tried the above and got back in both cases the first 2 of your "Should match" results. 使用MongoDB 2.4.5,我尝试了上述操作,并在两种情况下均返回了“应匹配”结果的前2个。

I don't believe the 3rd should be returned as it would require ("052049" >= "050600" && "052049" < "052000") or ("052420" < "052000" && "052420" > "050600") neither of which evaluate to true. 我不认为应该按要求返回第三位(“ 052049”> =“ 050600” &&“ 052049” <“ 052000”)或(“ 052420” <“ 052000” &&“ 052420”>“ 050600”)两者均未评估为真。

MongoDB shell version: 2.4.5
connecting to: test
> 
> db.mycol.save({ "start" : "050520", "end" : "051309" }) 
> db.mycol.save({ "start" : "051125", "end" : "051925" })
> db.mycol.save({ "start" : "052049", "end" : "052420" }) 
> db.mycol.save({ "start" : "050000", "end" : "050500" })
> db.mycol.save({ "start" : "052100", "end" : "052721" })
>
> db.mycol.find( 
... { $or : 
... [ { $and : [ { start : { $gte : "050600" } }, { start : { $lt : "052000" } } ] }, 
...   { $and : [ { end : { $lt : "052000" } }, { end : { $gt : "050600" } } ] } 
... ] 
...     } )
{ "_id" : ObjectId("520528522683a40682f12b5c"), "start" : "050520", "end" : "051309" }
{ "_id" : ObjectId("520528522683a40682f12b5d"), "start" : "051125", "end" : "051925" }
> 
> 
> db.mycol.find( 
... { $or : 
... [     { $and : [ { end : { $lt : "052000" } }, { end : { $gt : "050600" } } ] } , 
...   { $and : [ { start : { $gte : "050600" } }, { start : { $lt : "052000" } } ] }
... ] 
...     } )
{ "_id" : ObjectId("520528522683a40682f12b5c"), "start" : "050520", "end" : "051309" }
{ "_id" : ObjectId("520528522683a40682f12b5d"), "start" : "051125", "end" : "051925" }
> 

Your query should work fine, but the third one shouldn't match as both start and end are out of the range. 您的查询应该工作正常,但第三个不应该匹配既是startend都超出了范围。

However, you can write your query more clearly and succinctly as: 但是,您可以将查询更清楚,更简洁地编写为:

db.test.find({ $or: [
    { start: { $gte: "050600", $lt : "052000" }}, 
    { end: { $lt: "052000", $gt: "050600" }}
]});

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

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