mongodb queries both with AND and OR

后端 未结 4 1919
情歌与酒
情歌与酒 2020-12-13 00:10

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

the code below doesn\'t work as expected

db.things.find({
           $and:[
                {         


        
相关标签:
4条回答
  • 2020-12-13 00:19

    This is possible in following manner (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

    
            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.

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

    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

    Or

    location array in 588054c63879f2e767a1d553 and is_freebie

    0 讨论(0)
  • 2020-12-13 00:24

    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.

    Also check 10Gen's Advanced Query Documentation for $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 $and and your server version.

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

    -SethO

    0 讨论(0)
  • 2020-12-13 00:27

    Shorter to use an implicit AND operation:

    db.things.find({
        $or : [ 
            {"first_name": "john"},
            {"last_name": "john"}
        ],
        "phone": "12345678"         
    })
    
    0 讨论(0)
  • 2020-12-13 00:40
     db.things.find( {
          $and : [
                   { 
                     $or : [ 
                             {"first_name" : "john"},
                             {"last_name" : "john"}
                           ]
                   },
                   { 
                     "Phone":"12345678"
                   }
                 ]
        } )
    

    AND takes an array of 2 expressions OR , phone.
    OR takes an array of 2 expressions first_name , last_name.

    AND

    • OR

      • first_name
      • last_name
    • Phone Number.

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

    0 讨论(0)
提交回复
热议问题