Van Peer
Van Peer

Reputation: 2167

what creates the default ObjectID in mongodb?

Quite new to MongoDB. Going through the documentation.

From https://docs.mongodb.com/manual/reference/method/db.collection.insert/#id-field
If the document does not specify an _id field, then MongoDB will add the _id field and assign a unique ObjectId for the document before inserting. Most drivers create an ObjectId and insert the _id field, but the mongod will create and populate the _id if the driver or application does not.

Here it says MongoDB will create the _id.

From https://docs.mongodb.com/manual/reference/method/db.collection.save/?_ga=2.24854906.1181826191.1496665143-146886871.1493019056#save-a-new-document-without-specifying-an-id-field
During the insert, the shell will create the _id field with a unique ObjectId value, as verified by the inserted document.

And here, it says Mongo shell.

Is it the mongo shell that creates the default ObjectID or mongod? Hope someone can clarify!

Upvotes: 0

Views: 1741

Answers (2)

Udara Gunathilake
Udara Gunathilake

Reputation: 224

If the client didn't create Id for the document, mongo server will create the _id when it creates a new document.

Upvotes: 0

JohnnyHK
JohnnyHK

Reputation: 312139

If the client doesn't provide the _id, the MongoDB server will do it. The shell is just another type of client; it provides the _id for inserted documents, so the server doesn't need to. But other drivers/clients may not, so in those cases the server does.

Upvotes: 1

Related Questions