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

Multi-key indexes on arrays of object with more than one key cause queries to behave like $elemMatch by default.

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • None
    • None
    • Index Maintenance
    • None
    • ALL

    Description

      Attempting to index and query a set of dynamic attributes, I seem to have encountered a bug.

      > db.foo.save({attrs: [

      {n: "low", v: 100}

      ,

      {n: "high", v: 100}

      ]})
      > db.foo.save({attrs: [

      {n: "low", v: 200}

      ,

      {n: "high", v: 500}

      ]})
      > db.foo.find(

      {"attrs.n": "low", "attrs.v": 500}

      )
      { "_id" : ObjectId("4d66b232d6eb5559fa645ce2"), "attrs" : [

      { "n" : "low", "v" : 200 }

      ,

      { "n" : "high", "v" : 500 }

      ] }

      Now we add an index, and this returns nothing:

      > db.foo.ensureIndex(

      {"attrs.n": 1, "attrs.v": 1}

      )
      > db.foo.find(

      {"attrs.n": "low", "attrs.v": 500}

      )
      (Returns nothing)

      Shouldn't the behavior be the same with the index in place?

      However. A query matching a complete object with the document work with the index:
      > db.foo.find(

      {"attrs.n": "low", "attrs.v": 200}

      )
      { "_id" : ObjectId("4d66b232d6eb5559fa645ce2"), "attrs" : [

      { "n" : "low", "v" : 200 }

      ,

      { "n" : "high", "v" : 500 }

      ] }

      And seems to be doing the equivalent of an $elemMatch:
      > db.foo.find({attrs: {$elemMatch:

      {n: "low", v: 100}

      }})
      { "_id" : ObjectId("4d66b227d6eb5559fa645ce1"), "attrs" : [

      { "n" : "low", "v" : 100 }

      ,

      { "n" : "high", "v" : 100 }

      ] }

      Is this the expected behavior?

      Attachments

        Activity

          People

            aaron Aaron Staple
            kbanker Kyle Banker
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: