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"
}
]
}
5
Answers
Try:
This will generate the objectId on client side
I have tried
and it works fine for me
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.
Do it like this:
While this is pretty tedious, if you want to use the typical Meteor structure of a 17 character alphanumeric string, you can use a random text generator online to generate a string, then do a
.find()
in the collection to see if it exists, and then pass it in your.insert()
as the_id
value in your insert object. I had the same problem and this was the way I got around it.