Compound index on objects embedded in array can lead to inconsistent query results

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • ALL
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Creating a compound index on fields of objects embedded in an array can change the behavior of queries on those fields. It's easiest to demonstrate this behavior using an example. In the test case below, see that querying using the index produces no results, while the same query using no index produces one result.

      Test case:
      > db.test.insert({ "_id" : 1, "arr" : [

      { "animal" : "cat", "size" : "small" }

      ,

      { "animal" : "dog", "size" : "medium" }

      ]});;
      > db.test.ensureIndex(

      { "arr.animal" : 1, "arr.size" : 1 }

      );
      > db.test.find(

      { "arr.animal" : "cat", "arr.size" : "medium" }

      );
      > db.test.find(

      { "arr.animal" : "cat", "arr.size" : "medium" }

      ).hint(

      { "$natural" : 1 }

      );
      { "_id" : 1, "arr" : [

      { "animal" : "cat", "size" : "small" }

      ,

      { "animal" : "dog", "size" : "medium" }

      ] }

            Assignee:
            Unassigned
            Reporter:
            Bryon Ross
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: