top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How _id field is constructed in mongoDB ?

+2 votes
140 views
How _id field is constructed in mongoDB ?
posted Jun 13, 2015 by Vikram Singh

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

1 Answer

+1 vote

In RDMS, a primary key is used to identify a record uniquely. Similar to RDMS, mongoDB uses objectId to identify a document uniquely. In RDMS, a user takes care of primary key value while in case mongoDB, mongoDB server may take care to maintain uniqueness of a document.

" _id " field is used to identify a document uniquely in a database. _id is a 12-bytes long BSON type string.
These 12 bytes are constructed by following components.
a) First 4 bytes represents seconds since Unix epoch.
b) The next 3 bytes are machine identifiers.
c) The next 2 bytes consist of process Id.
d) Remaining 3 bytes are a random counter value.

answer Jun 13, 2015 by Alok
Similar Questions
0 votes

We all know that mongoDB provide different type of indexing (ascending, descending, geo2D, geo2d sphere, or text), so which type of indexing considered more efficient for indexing boolean field?

+2 votes

I get duplicates of fields and subfield using aggregation. Searching for duplicated sub field "k01.v" works fine:

db.table_0.aggregate({"$group" : { _id: "$k01.v", "count": { "$sum": 1 } } }
{"$match": {"count" : {"$gt": 1} } }
{"$project": {"k01.v" : "$_id", "_id" : 0} });

Unfortunately, this doesnt work when searching for sub field "a1.0" (field "a1" is an array):

db.table_0.aggregate({"$group" : { _id: "$a1.0", "count": { "$sum": 1 } } }
{"$match": {"count" : {"$gt": 1} } }
{"$project": {"a1.0" : "$_id", "_id" : 0} });

Any workaround ?

...