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

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Index Maintenance
    • None
    • ALL
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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?

            Assignee:
            Aaron Staple (Inactive)
            Reporter:
            Kyle Banker (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: