How to replace an item in an array in an aggregation?

Say I’d like to replace one ID by another inside an array (the targetfield )

const id1 = new ObjectId(), id2 = new ObjectId(), id3 = new ObjectId();
await coll.insertMany([
  { targets: [id1, id2] },
  { targets: [id1] },
  { targets: [id2] },
]);
await coll.aggregate([
  { $set: { target: {.... } } } // replace all id1 by id3
])
// desired output for coll.find()
/*[
  { targets: [id3, id2] },
  { targets: [id3] },
  { targets: [id2] },
]*/

Could you guide me on what the aggregation operator should be?

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