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

$elemMatch for querying nested objects

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
    • Environment:
      Feature missing from db version v1.9.0-pre-, pdfile version 4.5
      git version: c440cb9ddaa9cde91f58f35f24d6ac4d2583a016
    • Query

      Suppose you have several nested objects and lists:

      > db.test.insert({"a" : { "b" : { "c" :

      { "d1" : [ "e1" ], "d2" : [ "e2" ], "d3" : [ "e3", "e4" ], "d4" : [ "e5", "e6" ] }

      } } })
      > db.test.find({'a.b.c' : {$exists : true}})
      { "_id" : ObjectId("4daf2ccd697ebaacb10976ec"), "a" : { "b" : { "c" :

      { "d1" : [ "e1" ], "d2" : [ "e2" ], "d3" : [ "e3", "e4" ], "d4" : [ "e5", "e6" ] }

      } } }

      I want to retrieve this document based on the sub-object value:

      {'d2': ["e3":"e4"]}

      or just the existence of the array ["e1"] without knowing the value key 'd1'.
      The depth of the structure remains constant, but the keys are unknown.

      I thought that chaining a few $elemMatch would work, but it did not retrieve the desired results:

      > db.test.find({'a.b': {$elemMatch : {$elemMatch : {$all : ["e1"] }}}})
      > db.test.find({'a.b': {$elemMatch : {$elemMatch : {$elemMatch : {$all : ["e1"] }}}}})

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            dsmith Dustin Smith
            Votes:
            6 Vote for this issue
            Watchers:
            13 Start watching this issue

              Created:
              Updated:
              Resolved: