Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-1003

Add expression to get value by keyname from object

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.1.1
    • Affects Version/s: None
    • Component/s: Autocomplete
    • Labels:
    • Not Needed
    • Iteration Novokuybyshevsk

      Downstream Change Summary

      Added a new aggregation expression, $getField: see the syntax document for more details: https://docs.google.com/document/d/1aXTM207O4b8Sj7C76f3A44dvCIXErgKB_u17XLnsxfk

      Note that this is feature-flag guarded by featureFlagDotsAndDollars, which is currently disabled.

      Description of Linked Ticket

      While it's possible via combined use of $objectToArray and $filter with $map, it would be nice to have a simple expression to get value of particular field of an object:

      { _id: 1, a: 5, foo: { b: "a", c: 99.9 } }
      db.foo.aggregate({$project:{ foo_c_is: {$getField:[ "c", "$foo" ] } } })
      { _id: 1, foo_c_is: 99.9 }

      This will allow easily plucking out a value when the field name is stored in another part of the same document, or when it has some funny character that precludes it being used as subfield name (like leading "$") as in SERVER-30365 and SERVER-14466 or embedded '.' when querying oplog or system.profile collections.

      Second argument could be optional and default to "$$ROOT".

            alena.khineika@mongodb.com Alena Khineika
            jessica.sigafoos@mongodb.com Jessica Sigafoos
            0 Vote for this issue
            2 Start watching this issue