Merge object in array to array property

Hey ! Can anybody help me with getting 'mapname' : 'myName' on $project stage in pipeline ? I get those docs after ‘pipeline’ in $lookup stage:

[{
    "title": "e",
    "metadata": {
      "editedBy": "andre@rcs.org",
      "timeEdited": {
        "$date": "2022-08-23T00:46:01.783Z"
      },
      "createdBy": "andre@rcs.org",
      "timeCreated": {
        "$date": "2022-08-23T00:46:01.660Z"
      }
    },
    "mapname": [
      {
        "name": "myName"
      }
    ],
    "user_scenarios": []
  }]

Thanks in advance!

Untested possible solution:

{ "$set" : { "mapname" : "$mapname.0.name" } }

The possible solution is a stage in the aggregation pipeline. However, I feel that is it better to do this type of data cosmetic in application layer because it scale better. When done on the server, everybody is impacted by the extra work done on the server just to present the data in a specific format. When done in the application, only the instance of the application doing the query is impacted.

1 Like

Thank @steevej , here my solution based on your suggestion: { $set : { "mapname" : {$arrayElemAt: ['$mapname.name',0] } }} that gave me the string value

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.