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

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Backlog
    • Major - P3
    • Resolution: Unresolved
    • 1.6.2
    • None
    • Querying
    • Mac OSX 64bit
    • Query Optimization

    Description

      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.
      Case:

      // 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)

      Attachments

        Issue Links

          Activity

            People

              backlog-query-optimization Backlog - Query Optimization
              glennmoki Glenn Powell
              Votes:
              15 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated: