Tutorialspoint

1 Answer
Nishtha Thakur

To generate ObjectID, use the below syntax in the MonogDB shell −

new ObjectId()

Let us implement the above syntax to generate ObjectID in MongoDB −

> new ObjectId()
ObjectId("5cd7bf2f6d78f205348bc646")

> new ObjectId()
ObjectId("5cd7bf316d78f205348bc647")

> new ObjectId()
ObjectId("5cd7bf336d78f205348bc648")

> new ObjectId()
ObjectId("5cd7bf346d78f205348bc649")

> new ObjectId()
ObjectId("5cd7bf356d78f205348bc64a")

> new ObjectId()
ObjectId("5cd7bf396d78f205348bc64b")

> new ObjectId()
ObjectId("5cd7bf3a6d78f205348bc64c")

As shown above, every time you will get a new ObjectId.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.