0

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?

1
  • question is very similar to this Commented Apr 30, 2022 at 10:54

2 Answers 2

1

The code new mongo.ObjectId() will run on the driver => 1 id for all.
You need the js to run on the server, one time for each member.

Try to replace the new mongo.ObjectId() with the bellow $function code that will run on the server.
In general we should avoid javascript, but i don't think we have an aggregate operator that generates ObjectId's.

Another alternative is to write all the $map function in javascript, again with the use of $function.

*$function requires MongoDB >=4.4

{
  "$function": {
    "body": "function () {return new ObjectId();}",
    "args": [],
    "lang": "js"
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, though I am using Mongo v5.0 it's on a free Atlas cluster which does not allow server side JavaScript.
1

the _id field in a document cannot be modified.in fact you are tying to modify main objectid. try another name for your transaction_id field.

3 Comments

Could I use an ObjectId or should I generate a random number with Math.random() Or even better just increment by 1 for each object would be sufficient, I just need a way of identifying the object from another collection.
there is a trick like adding a number to end of main object id .it helps readability or other usages in the future that probably you dont know about right now.but since your purpose is only making a unique value as you said , then dont spend time to engineering a small feature. just use math.random and go to next step.
by the way math.random is math.random. its not math.unique ... !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.