Does Mongo DB create _id of document by itself?
This question often confuse some developers, how mongo db creates unique document _id so fast. But the truth is mongo db doesn’t create document _id by itself.
Let’s perform a test…- I create a model of Boy and then created an object of that model.
- I print that object on console before saving it on mongo db.
3. But console printed this result..
4. But this document got _id before saving it on database.
Once we create an object of mongoose model. Mongo generates an unique _id for that particular document before saving it on database. In fact it doesn’t care whether application is connected with database or not.
Unlike relational databases, the _id of document is created by mongo driver not by database under the hood. If database gets many requests at a single time to save documents, so there is no need to wait for generating an unique ID to save document. This makes mongo db highly scalable and more faster than other databases.
nice Article......
ReplyDelete