mongodb 使用 AND 和 OR 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11196101/
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 12:43:11  来源:igfitidea点击:

mongodb queries both with AND and OR

mongodb

提问by user1171632

can I use combination of OR and AND in mongodb queries?

我可以在mongodb 查询中使用OR 和AND 的组合吗?

the code below doesn't work as expected

下面的代码没有按预期工作

db.things.find({
           $and:[
                {$or:[
                     {"first_name" : "john"}, 
                     {"last_name" : "john"}
                ]},
                {"phone": "12345678"}
            ]});

database content:

数据库内容:

> db.things.find();
{ "_id" : ObjectId("4fe8734ac27bc8be56947d60"), "first_name" : "john", "last_name" : "hersh", "phone" : "2222" }
{ "_id" : ObjectId("4fe8736dc27bc8be56947d61"), "first_name" : "john", "last_name" : "hersh", "phone" : "12345678" }
{ "_id" : ObjectId("4fe8737ec27bc8be56947d62"), "first_name" : "elton", "last_name" : "john", "phone" : "12345678" }
{ "_id" : ObjectId("4fe8738ac27bc8be56947d63"), "first_name" : "eltonush", "last_name" : "john", "phone" : "5555" }

when querying the above query - i get nothing!

查询上述查询时 - 我什么也没得到!

> db.things.find({$and:[{$or:[{"first_name" : "john"}, {"last_name" : "john"}]},{"phone": "12345678"}]});
>

I'm using mongo 1.8.3

我正在使用 mongo 1.8.3

采纳答案by SethO

I believe $and is only supported in MongoDB v2.0+. I'm surprised that the console accepted the expression in the first place. I'll try to recreate against a 1.8 server I have.

我相信 $and 仅在 MongoDB v2.0+ 中受支持。我很惊讶控制台首先接受了这个表达式。我将尝试针对我拥有的 1.8 服务器重新创建。

Also check 10Gen's Advanced Query Documentationfor $and.

另请查看10Gen 的 Advanced Query Documentationfor $and.

Update

更新

I entered your sample data into both a v1.8x and v2.0x MongoDB server. The query works on the v2.0x and fails on the v1.8x. This confirms my (and Miikka's) suspicions that the problem is with $andand your server version.

我将您的示例数据输入到 v1.8x 和 v2.0x MongoDB 服务器中。查询在 v2.0x 上有效,在 v1.8x 上失败。这证实了我(和 Miikka)怀疑问题出在$and您的服务器版本上。

I found a (arguably) clever workaround for this on the Web here. I hope this helps.

我发现这是一个(可以说)聪明的解决办法在网络上点击这里。我希望这有帮助。

-SethO

-SethO

回答by its4zahoor

 db.things.find( {
      $and : [
               { 
                 $or : [ 
                         {"first_name" : "john"},
                         {"last_name" : "john"}
                       ]
               },
               { 
                 "Phone":"12345678"
               }
             ]
    } )

ANDtakes an array of 2 expressions OR, phone.
ORtakes an array of 2 expressions first_name , last_name.

AND需要一个包含 2 个表达式OR的数组,电话。
OR采用 2 个表达式的数组 first_name , last_name 。

AND

  • OR

    • first_name
    • last_name
  • Phone Number.

  • 或者

  • 电话号码。

Note: Upgrade to latest version of MongoDB, if this doesn't work.

注意:如果这不起作用,请升级到最新版本的 MongoDB。

回答by TechWisdom

Shorter to use an implicit AND operation:

更短的使用隐式 AND 运算:

db.things.find({
    $or : [ 
        {"first_name": "john"},
        {"last_name": "john"}
    ],
    "phone": "12345678"         
})

回答by Chirag Purohit

This is possible in following manner (mongodb 3.4)

这可以通过以下方式(mongodb 3.4)

Here is the good example

这是一个很好的例子

https://docs.mongodb.com/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-operator

https://docs.mongodb.com/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-operator

        db.getCollection('tbl_menu_items').find({
        $and : [
            { $or : [ { price : 0.99 }, { price : 1.99 } ] },
            { $or : [ { sale : true }, { qty : { $lt : 20 } } ] }
        ]
    } )

This query will select all documents where:

此查询将选择所有文档,其中:

the price field value equals 0.99 or 1.99, and the sale field value is equal to true or the qty field value is less than 20.

price 字段值等于 0.99 或 1.99,而 sale 字段值等于 true 或 qty 字段值小于 20。

Here is the example of $and condition with $or query i.e matching any of condition for example

这是 $and 条件与 $or 查询的示例,即匹配任何条件,例如

consider following query...

考虑以下查询...


        db.getCollection('tbl_menu_items').find(

        { '$or': 
           [ { '$and': 
                [ { location: { '$in': [ ObjectId("588054c63879f2e767a1d553")  ] } },
                  { is_ryward: 1 },
                  { points_reqiured_to_redeem_reward: { '$lte': 500 } } ] },
             { '$and': 
                [ { location: { '$in': [ ObjectId("588054c63879f2e767a1d553")  ] } },
                  { is_freebie: 1 } ] } ] }
        )



This query will select all documents where:

此查询将选择所有文档,其中:

location array in 588054c63879f2e767a1d553 and is_ryward = 1 and points_reqiured_to_redeem_reward < 500

位置数组在 588054c63879f2e767a1d553 和 is_ryward = 1 和 points_reqiured_to_redeem_reward < 500

Or

或者

location array in 588054c63879f2e767a1d553 and is_freebie

588054c63879f2e767a1d553 和 is_freebie 中的位置数组