top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How explicitly give the object id in mongoDB?

+2 votes

objectId in mongoDB is of total 12 bytes hexadecimal number is unique for every document in a collection. which is a comobination of 4 bytes timestamp, 3 bytes of machine id, 2 bytes of process id and 3 bytes of incrementer or random counter.

if we do not mention the _id field explicitly it is inserted by the sever itself.

if one wants to insert the _id field, how it should be constructed explicitly??


   _id: ObjectId(7df78ad8902c),
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by: 'Query Home',
   url: '',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100

In the above example _id: ObjectId(7df78ad8902c) this bold part. how this 12 bytes id is constructed manually??

posted Jun 20, 2015 by Prakash Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Most language drivers will add the _id field on insert if the document to insert does not already include it. If a driver doesn't do this the server will add _id to the inserted document. All official MongoDB drivers have an ObjectId implementation you can use to generate _id values, but note that _id does not have to be and instance of ObjectId. It just has to be a unique value:

answer Jun 22, 2015 by anonymous
Similar Questions
0 votes

My collection is like this, but I want to update a nested object with {"env":"qa"} in my updated collection with java-driver:

{ "_id" : ObjectId("5b052eeff9290437b217b1ed"), "app" : "hike", "group" : [ { "env" : "prod" }, { "env" : "test" } ]}{ "_id" : ObjectId("5b052f36f9290437b217b1ee"), "app" : "viber", "group" : [ { "env" : "prod" }, { "env" : "test" } ]}