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

Indexes causes incorrect queries on 2.6.1

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.6.1
    • Component/s: Querying
    • None
    • ALL
    • Hide
      // This is a simplified harness to replicate what we're seeing in our DB
      
      // Set up data
      
      /** number_user_id indexes **/
      db.getCollection("number_user_id").ensureIndex({
        "_id": NumberInt(1)
      },[]);
      
      /** number_user_id indexes **/
      db.getCollection("number_user_id").ensureIndex({
        "user_id": NumberLong(1)
      },[]);
      
      /** number_user_id indexes **/
      db.getCollection("number_user_id").ensureIndex({
        "number": NumberLong(1)
      },[]);
      
      /** number_user_id indexes **/
      db.getCollection("number_user_id").ensureIndex({
        "user_id": NumberLong(1),
        "number": NumberLong(1)
      },{
        "unique": true
      });
      
      /** number_user_id records **/
      db.getCollection("number_user_id").insert({
        "_id": ObjectId("537dc86a80f1cf0e42d63af1"),
        "number": null,
        "user_id": 1
      });
      db.getCollection("number_user_id").insert({
        "_id": ObjectId("537dc87f80f1cf0f42d63af1"),
        "number": 2,
        "user_id": 1
      });
      db.getCollection("number_user_id").insert({
        "_id": ObjectId("537dc87580f1cf0d42d63af1"),
        "number": 1,
        "user_id": 1
      });
      
      // Queries
      
      db.number_user_id.find({ $or: [{ number: null }, { number: { $lte: 2 } }] });
      // On 2.4 you get all 3 documents back on 2.6 you get 1 document back
      
      db.number_user_id.find({ $or: [{ number: null }, { number: { $lte: 2 } }] }).count();
      // On 2.4 you get 3 on 2.6 you get 1
      
      Show
      // This is a simplified harness to replicate what we're seeing in our DB // Set up data /** number_user_id indexes **/ db.getCollection("number_user_id").ensureIndex({ "_id": NumberInt(1) },[]); /** number_user_id indexes **/ db.getCollection("number_user_id").ensureIndex({ "user_id": NumberLong(1) },[]); /** number_user_id indexes **/ db.getCollection("number_user_id").ensureIndex({ "number": NumberLong(1) },[]); /** number_user_id indexes **/ db.getCollection("number_user_id").ensureIndex({ "user_id": NumberLong(1), "number": NumberLong(1) },{ "unique": true }); /** number_user_id records **/ db.getCollection("number_user_id").insert({ "_id": ObjectId("537dc86a80f1cf0e42d63af1"), "number": null, "user_id": 1 }); db.getCollection("number_user_id").insert({ "_id": ObjectId("537dc87f80f1cf0f42d63af1"), "number": 2, "user_id": 1 }); db.getCollection("number_user_id").insert({ "_id": ObjectId("537dc87580f1cf0d42d63af1"), "number": 1, "user_id": 1 }); // Queries db.number_user_id.find({ $or: [{ number: null }, { number: { $lte: 2 } }] }); // On 2.4 you get all 3 documents back on 2.6 you get 1 document back db.number_user_id.find({ $or: [{ number: null }, { number: { $lte: 2 } }] }).count(); // On 2.4 you get 3 on 2.6 you get 1

      Upgrading from 2.4.10 to 2.6.1 (running on OS X 10.9.2) found running an $or query with a nested $lte on indexed fields causes incorrect query results.

      db.number_user_id.find({ $or: [{ number: null }, { number: { $lte: 2 } }] });
      // On 2.4 you get all 3 documents back on 2.6 you get 1 document back
      

      I posted this on Stack Overflow (http://stackoverflow.com/questions/23805916/mongodb-indexed-queries-returning-incorrect-values-since-upgrading-to-2-6-from-2) and got a couple of answers back suggesting it was a bug, with one saying it works with a sparse index.

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            kevindew Kevin Dew
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: