Adding compound index changes result set of query

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.6.1
    • Component/s: Querying, Shell
    • None
    • ALL
    • Hide

      Create collection:

      db.getCollection("types").insert({
        "domainId": "type",
      });
      db.getCollection("types").insert({
        "domainId": "view",
      });
      db.getCollection("types").insert({
        "domainId": "vfsdata",
        "namespace": "eportal",
      });
      db.getCollection("types").insert({
        "domainId": "admin",
        "namespace": "eportal",
      });
      

      Test query:

      db.types.find( { "$or" : [ { "namespace" : "eportal"} , { "namespace" :  null }] } )
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c13"), "domainId" : "type" }
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c14"), "domainId" : "view" }
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c15"), "domainId" : "vfsdata", "namespace" : "eportal" }
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c16"), "domainId" : "admin", "namespace" : "eportal" }
      

      Add index:

      db.types.ensureIndex( { namespace: 1, domainId: 1 }, { unique: 1 } )
      

      Test query again:

      db.types.find( { "$or" : [ { "namespace" : "eportal"} , { "namespace" :  null }] } )
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c13"), "domainId" : "type" }
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c14"), "domainId" : "view" }
      
      Show
      Create collection: db.getCollection("types").insert({ "domainId": "type", }); db.getCollection("types").insert({ "domainId": "view", }); db.getCollection("types").insert({ "domainId": "vfsdata", "namespace": "eportal", }); db.getCollection("types").insert({ "domainId": "admin", "namespace": "eportal", }); Test query: db.types.find( { "$or" : [ { "namespace" : "eportal"} , { "namespace" : null }] } ) { "_id" : ObjectId("538602b59b6fe0b5efcf8c13"), "domainId" : "type" } { "_id" : ObjectId("538602b59b6fe0b5efcf8c14"), "domainId" : "view" } { "_id" : ObjectId("538602b59b6fe0b5efcf8c15"), "domainId" : "vfsdata", "namespace" : "eportal" } { "_id" : ObjectId("538602b59b6fe0b5efcf8c16"), "domainId" : "admin", "namespace" : "eportal" } Add index: db.types.ensureIndex( { namespace: 1, domainId: 1 }, { unique: 1 } ) Test query again: db.types.find( { "$or" : [ { "namespace" : "eportal"} , { "namespace" : null }] } ) { "_id" : ObjectId("538602b59b6fe0b5efcf8c13"), "domainId" : "type" } { "_id" : ObjectId("538602b59b6fe0b5efcf8c14"), "domainId" : "view" }
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None

      I have a simple collection with these values:

      { "_id" : ObjectId("538602b59b6fe0b5efcf8c13"), "domainId" : "type" }
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c14"), "domainId" : "view" }
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c15"), "domainId" : "vfsdata", "namespace" : "eportal" }
      { "_id" : ObjectId("538602b59b6fe0b5efcf8c16"), "domainId" : "admin", "namespace" : "eportal" }
      

      As expected the query

      "$or" : [ { "namespace" : "eportal"} , { "namespace" :  null }]
      

      found all documents in the collection.
      If i add a unique compound index for the fields namespace and eportal, the same query found only the documents, where the field namespace exists.
      In version 2.4 of MongoDB the second query found as expected all documents.

            Assignee:
            David Storch
            Reporter:
            Michael Schoene
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: