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

Adding compound index changes result set of query

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • None
    • 2.6.1
    • 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" }

    Description

      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.

      Attachments

        Activity

          People

            david.storch@mongodb.com David Storch
            mrs2207 Michael Schoene
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: