带有“或”条件的 MongoDB 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2008032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:41:37  来源:igfitidea点击:

MongoDB query with an 'or' condition

mongodb

提问by SFEley

So I have an embedded document that tracks group memberships. Each embedded document has an ID pointing to the group in another collection, a start date, and an optionalexpire date.

所以我有一个跟踪组成员身份的嵌入式文档。每个嵌入的文档都有一个指向另一个集合中的组的 ID、一个开始日期和一个可选的到期日期。

I want to query for current members of a group. "Current" means the start time is less than the current time, and the expire time is greater than the current time OR null.

我想查询组的当前成员。“当前”表示开始时间小于当前时间,过期时间大于当前时间或为空。

This conditional query is totally blocking me up. I could do it by running two queries and merging the results, but that seems ugly and requires loading in all results at once. Or I could default the expire time to some arbitrary date in the far future, but that seems even uglier and potentially brittle. In SQL I'd just express it with "(expires >= Now()) OR (expires IS NULL)" -- but I don't know how to do that in Mongo.

这个条件查询完全阻止了我。我可以通过运行两个查询并合并结果来做到这一点,但这看起来很难看,并且需要一次加载所有结果。或者我可以将过期时间默认为遥远未来的某个任意日期,但这似乎更丑陋且可能脆弱。在 SQL 中,我只是用“(expires >= Now()) OR (expires IS NULL)”来表达它——但我不知道如何在 Mongo 中做到这一点。

Any ideas? Thanks very much in advance.

有任何想法吗?首先十分感谢。

回答by mstearn

Just thought I'd update in-case anyone stumbles across this page in the future. As of 1.5.3, mongo now supports a real $or operator: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24or

只是想我会更新以防万一将来有人偶然发现此页面。从 1.5.3 开始,mongo 现在支持真正的 $or 运算符:http: //www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24or

Your query of "(expires >= Now()) OR (expires IS NULL)" can now be rendered as:

您对“(expires >= Now()) OR (expires IS NULL)”的查询现在可以呈现为:

{$or: [{expires: {$gte: new Date()}}, {expires: null}]}

回答by robertjmoore

In case anyone finds it useful, www.querymongo.comdoes translation between SQL and MongoDB, including OR clauses. It can be really helpful for figuring out syntax when you know the SQL equivalent.

如果有人觉得它有用,www.querymongo.com会在 SQL 和 MongoDB 之间进行翻译,包括 OR 子句。当您知道 SQL 等价物时,它对于弄清楚语法非常有帮助。

In the case of OR statements, it looks like this

在 OR 语句的情况下,它看起来像这样

SQL:

查询语句:

SELECT * FROM collection WHERE columnA = 3 OR columnB = 'string';

MongoDB:

MongoDB:

db.collection.find({
    "$or": [{
        "columnA": 3
    }, {
        "columnB": "string"
    }]
});

回答by nont

Query objects in Mongo by default AND expressions together. Mongo currently does not include an OR operator for such queries, however there are ways to express such queries.

查询 Mongo 中的对象默认将 AND 表达式放在一起。Mongo 目前不包含用于此类查询的 OR 运算符,但是有多种方法可以表达此类查询。

Use "in" or "where".

使用“in”或“where”。

Its gonna be something like this:

它会是这样的:

db.mycollection.find( { $where : function() { 
return ( this.startTime < Now() && this.expireTime > Now() || this.expireTime == null ); } } );

回答by Abilash Raghu

db.Lead.find(

db.Lead.find(

{"name": {'$regex' : '.*' + "Ravi" + '.*'}},
{
"$or": [{
    'added_by':"[email protected]"
}, {
    'added_by':"[email protected]"
}]
}

);

);

回答by mstearn

Using a $wherequery will be slow, in part because it can't use indexes. For this sort of problem, I think it would be better to store a high value for the "expires" field that will naturally always be greater than Now(). You can either store a very high date millions of years in the future, or use a separate type to indicate never. The cross-type sort order is defined at here.

使用$where查询会很慢,部分原因是它不能使用索引。对于这类问题,我认为最好为“过期”字段存储一个高值,该值自然总是大于 Now()。您可以存储未来数百万年的非常高的日期,也可以使用单独的类型来表示从不。跨类型排序顺序在此处定义。

An empty Regex or MaxKey (if you language supports it) are both good choices.

空的 Regex 或 MaxKey(如果您的语言支持)都是不错的选择。