Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-13484

Implied $and in find query doesn't work consistently

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • ALL
    • Hide

      In a mongo shell:

      db.doubleID.insert(_id:2,column1:2)
      db.doubleID.find({ $or: [ { _id: { $lte: 1.0 } }, { _id: { $lte: "1" } } ], $or: [ { _id: { $gte: 1.0 } }, { _id: { $gte: "1" } } ] })
      db.doubleID.find({ $or: [ { _id: { $gte: 1.0 } }, { _id: { $gte: "1" } } ], $or: [ { _id: { $lte: 1.0 } }, { _id: { $lte: "1" } } ] })
      

      The first find returns 1 row, the second find returns 0 rows.

      Explicitly stating the $and seems to work as expected:

      db.doubleID.find({ $and: [ {$or: [ { _id: { $lte: 1.0 } }, { _id: { $lte: "1" } } ]}, {$or: [ { _id: { $gte: 1.0 } }, { _id: { $gte: "1" } } ]}] })
      

      This bug is not specific to the _id column, this also returns one row:

      db.doubleID.find({ $or: [ { column1: { $lte: 1.0 } }, { column1: { $lte: "1" } } ], $or: [ { column1: { $gte: 1.0 } }, { column1: { $gte: "1" } } ] })
      
      Show
      In a mongo shell: db.doubleID.insert(_id:2,column1:2) db.doubleID.find({ $or: [ { _id: { $lte: 1.0 } }, { _id: { $lte: "1" } } ], $or: [ { _id: { $gte: 1.0 } }, { _id: { $gte: "1" } } ] }) db.doubleID.find({ $or: [ { _id: { $gte: 1.0 } }, { _id: { $gte: "1" } } ], $or: [ { _id: { $lte: 1.0 } }, { _id: { $lte: "1" } } ] }) The first find returns 1 row, the second find returns 0 rows. Explicitly stating the $and seems to work as expected: db.doubleID.find({ $and: [ {$or: [ { _id: { $lte: 1.0 } }, { _id: { $lte: "1" } } ]}, {$or: [ { _id: { $gte: 1.0 } }, { _id: { $gte: "1" } } ]}] }) This bug is not specific to the _id column, this also returns one row: db.doubleID.find({ $or: [ { column1: { $lte: 1.0 } }, { column1: { $lte: "1" } } ], $or: [ { column1: { $gte: 1.0 } }, { column1: { $gte: "1" } } ] })

      Changing the order of the clauses in a query object can change the result returned.

      In the sample below, I reversed the order of the two $or clauses.

      It looks like this is in 2.4.6 through 2.4.9, I haven't tested other server versions yet.

      http://docs.mongodb.org/master/reference/operator/query/and/#op._S_and documents an implicit AND. The behaviour should probably be changed to match the documentation.

            Assignee:
            Unassigned Unassigned
            Reporter:
            GerryF Gerry F
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: