I need to assign a unique Object Id to each transaction Array Object in an existing document.
{
_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"
}]
}
My goal is for each transactions Object to have its own unique ObjectId, but the current query I am using assigns the same ObjectId to all objects:
collection.updateMany({}, [
{
$set: {
transactions: {
$map: {
input: "$transactions",
in: {
$mergeObjects: ["$$this", { _id: new mongo.ObjectId() }],
},
},
},
},
},
]);
I'm trying to figure out why I'm not getting unique ObjectIds for each object in the transactions array. Any insights?