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

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

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

      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 Unassigned
            Reporter:
            bross@playdom.com Bryon Ross
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: