I have an existing document and I wish to add a unique Object Id to each of the transaction Array Objects.
{
_id: ObjectId(6086d7e7e39add6a5220d0a5),
firstName: "John"
lastName: "Doe"
transactions: [
{
date: 2022-04-12T09:00:00.000+00:00
amount: 286.56
type: "deposit"
method: "Bank transfer"
},
{
date: 2022-04-15T09:00:00.000+00:00
amount: 120.23
type: "withdrawal"
method: "cash"
}]
}
The outcome would be each transactions Object would have a unique ObjectId. I have tried the following query but it add the same ObjectId to all objects:
collection.updateMany({}, [
{
$set: {
transactions: {
$map: {
input: "$transactions",
in: {
$mergeObjects: ["$$this", { _id: new mongo.ObjectId() }],
},
},
},
},
},
]);
Can anyone see why I am getting the same ObjectId written to all of the Objects in the transactions array instead of unique Ids?