Creating Documents: insertOne()

In the quiz related to the insertOne() there is the statement

MongoDB or the client will create an _id for us if we do not supply one.

I was perplexed by the client part of this statement. I am fully on board with MongoDB database creating the _id, if we don’t supply one, but what is this client part? In my experience it is all the database, that hands out the unique id’s in this case.

I appreciate that the video states a client will create one @ 5:20 mins.

1 Like

Interesting question! I’m looking forward to the answer from the MongoU folks. :slight_smile: I’ll also poke around a little bit, see what I can come up with.

EDIT:
BOOM! One Google search later (_mongodb "id" client generate) gives me the following blog post.

-> https://www.mongodb.com/blog/post/generating-globally-unique-identifiers-for-use-with-mongodb

That explains most of what you’re looking for. There are Mongo-supported, official methods of generating the objectID in non-standard methods.

@NMullins I think It just means that whatever client (aka program) that is generating the document and calling the MongoDB API’s can specify its own unique ID if they do not want MongoDB to do it for you. In Chapter 2 there is an example where the instructor uses the IMDB id as the _id value If I recall. Of course that leaves it up to the client (and programmers) to make sure the ID’s remain unique, etc…

2 Likes

Mike_67094

The end of the statement, has the bit about “if we do not supply one”, so in the example in the video you are talking about, he was supplying the imdb reference as the _id, so that doesn’t really qualify as "if we do not supply one.

Thanks for the link.

So apart from the MongoDB driver creating the unique id, the other mechanisms are really creating the _id via various program techniques, counters etc. It is interesting that it’s the mongodb driver creates the _id, and not the driver requesting the _id from the db.

I would just like it clarified by Mongo, what is meant by client in the above statement.

I am at the mongodb Europe conference tomorrow, so I will have a chat with the technical staff and see if I can get it clarified to my satisfaction.

1 Like

So when I spoke to mongoDB, they clarified that “client” in the statement below:

_MongoDB or the client will create an id for us if we do not supply one.

Was the client drivers, such as NodeJS, Java etc. So the actual MongoDB database or the client drivers can create the _Id’s if we don’t supply them.

1 Like