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

Using dot-notation to field-select an element by position from a nested Array fails to return the element



    • Type: New Feature
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: 1.6.2
    • Fix Version/s: Backlog
    • Component/s: Querying
    • Environment:
      Mac OSX 64bit


      It seems reasonable that if dot-notation works as a field-selector when selecting object members, that it should also work when selecting array indexes,
      but that doesn't seem to be the case with the current version of MongoDB.

      // Get the entire array (succeeds):
      > db.foo.find({"key":"12345"}, {"array_field"})
      { "_id" : ObjectId("4c9a41204cb16aa4fe28c4cc"), "array_field" : [
                      "bar" : "data",
                      "bam" : "more data",
      ] }
      // Attempt to get first element in array (index 0):
      > db.foo.find({"key":"12345"}, {"array_field.0":1})
      { "_id" : ObjectId("4c9a41204cb16aa4fe28c4cc"), "array_field" :
      [ { } ] } 

      The second command fails to return anything. You can in fact use the $slice operator to get the element, but then the command becomes large and ugly:

      > db.foo.find({"key":"12345"}, {_id:1, array_field:{$slice:[0,1]}}) 

      When developing custom ORM classes to access Mongo, this special case will be needed when accessing an array element by index. This seems unnecessary.
      I suggest that Mongo handle dot-notation the same for Arrays as it already does for Objects, and simply allow you to select by index. (Note, that a find query does allow array indexes in the dot notation, but the field select param does not)


          Issue Links



              • Votes:
                16 Vote for this issue
                9 Start watching this issue


                • Created: