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

Projection of field in a concrete array element (dot notation) returns element as empty object

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.4.10, 2.6.4
    • Component/s: Querying
    • None
    • ALL
    • Hide
      > db.projection_test.insert({ "some_field" : "some_value1", "my_array" : [ { "num_field" : 100, "s_field" : "aaa" }, { "num_field" : 100, "s_field" : "bbb" } ] })
      WriteResult({ "nInserted" : 1 })
      > db.projection_test.insert({ "some_field" : "some_value2", "my_array" : [ { "num_field" : 500, "s_field" : "aaa" }, { "num_field" : 100, "s_field" : "bbb" } ] })
      WriteResult({ "nInserted" : 1 })
      > db.projection_test.insert({ "some_field" : "some_value3", "my_array" : [ { "num_field" : 250, "s_field" : "aaa" }, { "num_field" : 100, "s_field" : "bbb" } ] })
      WriteResult({ "nInserted" : 1 })
      >
      

      Now, let's run the query with the projection:

      > db.projection_test.find({ "my_array.0.num_field": { $lt: 300 } }, {"some_field": 1, "my_array.0.num_field": 1})
      { "_id" : ObjectId("53f9e04d7c6631fa3b232bb0"), "some_field" : "some_value1", "my_array" : [ {  }, {  } ] }
      { "_id" : ObjectId("53f9e0617c6631fa3b232bb2"), "some_field" : "some_value3", "my_array" : [ {  }, {  } ] }
      > 
      

      So, the query match on the field of the concretely named array element works, but the projection clearly doesn't. It returns empty objects for the elements "my_array.0".

      Show
      > db.projection_test.insert({ "some_field" : "some_value1" , "my_array" : [ { "num_field" : 100, "s_field" : "aaa" }, { "num_field" : 100, "s_field" : "bbb" } ] }) WriteResult({ "nInserted" : 1 }) > db.projection_test.insert({ "some_field" : "some_value2" , "my_array" : [ { "num_field" : 500, "s_field" : "aaa" }, { "num_field" : 100, "s_field" : "bbb" } ] }) WriteResult({ "nInserted" : 1 }) > db.projection_test.insert({ "some_field" : "some_value3" , "my_array" : [ { "num_field" : 250, "s_field" : "aaa" }, { "num_field" : 100, "s_field" : "bbb" } ] }) WriteResult({ "nInserted" : 1 }) > Now, let's run the query with the projection: > db.projection_test.find({ "my_array.0.num_field" : { $lt: 300 } }, { "some_field" : 1, "my_array.0.num_field" : 1}) { "_id" : ObjectId( "53f9e04d7c6631fa3b232bb0" ), "some_field" : "some_value1" , "my_array" : [ { }, { } ] } { "_id" : ObjectId( "53f9e0617c6631fa3b232bb2" ), "some_field" : "some_value3" , "my_array" : [ { }, { } ] } > So, the query match on the field of the concretely named array element works, but the projection clearly doesn't. It returns empty objects for the elements "my_array.0".

      When you query for documents that contain an array whose elements themselves are (embedded) objects, and then you try to project a field of a concretely named array element (dot notation) into the result set, the query engine doesn't return that field but rather returns that array element as empty object instead.

            Assignee:
            ramon.fernandez@mongodb.com Ramon Fernandez Marina
            Reporter:
            tomzahn Thomas Zahn
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: