Pavel Alekseev
Pavel Alekseev

Reputation: 1222

How to merge two arrays of objects by the same field using MongoDB aggregation?

I have two arrays:

[
  {
    name: 'First object',
    value: 1
  },
  {
    name: 'Second object',
    value: 2
  },
  {
    name: 'Third object',
    value: 3
  },
];

and

[
  {
    name: 'First object',
    anotherValue: 1,
  },
  {
    name: 'Second object',
    anotherValue: 2,
  },
  {
    name: 'Third object',
    anotherValue: 3,
  },
];

Could I get new array using these two using MongoDB aggregation? I mean:

[
  {
    name: 'First object',
    value: 1,
    anotherValue: 1,
  },
  {
    name: 'Second object',
    value: 2,
    anotherValue: 2,
  },
  {
    name: 'Third object',
    value: 3,
    anotherValue: 3,
  },
];

I've tried to use facet, concatArrays and group to get that array but it doesn't work:

{
  $facet: {
    $firstArray: [...],
    $secondArray: [...],
  }
  $project: {
    mergedArray: {
      $concatArrays: [firstArray, secondArray],
    }
  },
  $group: {
    _id: {
      name: '$mergedArray.name'
    },
    value: {
      $first: '$mergedArray.value'
    },
    anotherValue: {
      $first: '$mergedArray.anotherValue'
    }
  },
}

But $anotherValue is always null.

Is there a neat way of achieving this by the aggregation framework?

Upvotes: 4

Views: 3982

Answers (1)

Valijon
Valijon

Reputation: 13103

To match by name, run the query below:

db.collection.aggregate([
  {
    $project: {
      anotherValue: {
        $map: {
          input: "$firstArray",
          as: "one",
          in: {
            $mergeObjects: [
              "$$one",
              {
                $arrayElemAt: [
                  {
                    $filter: {
                      input: "$secondArray",
                      as: "two",
                      cond: { $eq: ["$$two.name", "$$one.name"]}
                    }
                  },
                  0
                ]
              }
            ]
          }
        }
      }
    }
  }
])

MongoPlayground | Arrays with the same position + name value

Upvotes: 11

Related Questions