I'm trying to generate ObjectId() when inserting manually in Robo 3T.
Code below seems doesn't work. I wanted every object inside TestArray have a unique id.
How do I generate ObjectId manually?
{
"Name" : "Test",
"TestArray" : [
{
"_id" : ObjectId(),
"Name" : "Test"
}
]
}
CodePudding user response:
I have tried
x=ObjectId();
and it works fine for me
CodePudding user response:
Try:
new ObjectId()
This will generate the objectId on client side
CodePudding user response:
You don't have to generate the _id, just don't that field in your insert query and mongo will automatically generate it for you.
CodePudding user response:
Do it like this:
var TestArray = []
for (let i = 0; i < 10; i )
TestArray.push({ "_id": ObjectId(), "Name": "Test" })
{
"Name" : "Test",
"TestArray" : TestArray
}