-1

I have this document:

[{
  _id: "5a9eff23b9eea111a00016c8",
  participants: [
     2,
     3
  ]
}]

I would like to know if it's possible to add and format fields on array result, something like this.

{
  _id: "5a9eff23b9eea111a00016c8",
  participants: [
     {
      _id: 2,
      order: 22,
     },
     { 
      _id: 3,
      order: 22,
     } 
  ]
}

This should not be saved in the database, it should be only attached on response.

If it's possible, I would like an advice.

@rollstuhlfahrer Well , i don't think so , these fields ( order , _id) , doesn't exists in other table , they are custom

Dorin Musteața
  • 162
  • 1
  • 12

1 Answers1

0

I've figured it out. Works well. Done in PHP.

I've used $map , see the documentation below. https://docs.mongodb.com/manual/reference/operator/aggregation/map/

[
'$project' => [
'_id' => 0,
'title' => '$title',
'participants' => [
        '$map' => [
            'input' => '$participants',
            'as' => 'part',
            'in' => [
                'recipient' => '$$part',
                'status' => false,
            ]
        ]
    ]
]]

Here is the result:

{
  title: "Channel#O4ScAPkYLz",
  participants: [
    {
     recipient: 2,
     status: false
    },
    {
     recipient: 1,
     status: false
    }
  ]
}
Dorin Musteața
  • 162
  • 1
  • 12