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

search order for embedded documents, and only there

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

      When searching for a documents with multiple fields, the order of the fields doesn't matter: Querying for

      {color: 'blue', taste: 'salty'}
      

      will return documents that have {color: 'blue', taste: 'salty'} as well as documents that have {taste: 'salty', color: 'blue'}. That's consistent, for example, with lodash/underscore's find [1] and any other find in JS objects I know of.

      When it comes to embedded documents, though, mongodb takes a different route:

      > Equality matches on an embedded document require an exact match of the specified <value>, including the field order.

      (from [2]). This means that the query

      {specs: {color: 'blue', taste: 'salty'}}
      

      will NOT return documents with

      {specs: {taste: 'salty', color: 'blue'}}
      

      While it is possible to construct element matches that disregard field order [3], the described default behavior for embedded fields is inconsistent with document-level queries. This leads to application errors and in fact regularly bites people [4,5].

      Opinions?

      [1] https://lodash.com/docs#find
      [2] https://docs.mongodb.org/manual/tutorial/query-documents/#exact-match-on-the-embedded-document
      [3] https://docs.mongodb.org/manual/tutorial/query-documents/#equality-match-on-fields-within-an-embedded-document
      [4] http://stackoverflow.com/q/16002659/353337
      [5] https://twitter.com/andrenarchy/status/697041934699917312

            Assignee:
            Unassigned Unassigned
            Reporter:
            nico.schloemer@gmail.com Nico Schlömer
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: