[MongoDB] : Understanding ObjectId() vs UUID()

ObjectId() Returns a new ObjectId value. The 12-byte ObjectId value consists of:

a 4-byte value representing the seconds since the Unix epoch,
a 5-byte random value, and
a 3-byte counter, starting with a random value.

ObjectId() can accept the following parameter:

Field Type Description
hexadecimal String Optional. Hexadecimal string value for the new ObjectId.

Methods and Attributes :

ObjectId() has the following attribute and methods:

Attribute/Method          Description

str                       Returns the hexadecimal string representation of the object.
ObjectId.getTimestamp()   Returns the timestamp portion of the object as a Date.
ObjectId.toString()       Returns the JavaScript representation in the form of a string literal “ObjectId(…)”.
ObjectId.valueOf()        Returns the representation of the object as a hexadecimal string. The returned string is the str attribute.

> ISODate()
ISODate("2018-12-05T17:04:04.013Z")
> 
> ObjectId()
ObjectId("5c0805094b62a2bd736fe79c")
> 
> x = ObjectId()
ObjectId("5c0805134b62a2bd736fe79d")
> 
> ObjectId("5c0805134b62a2bd736fe79d").getTimestamp()
ISODate("2018-12-05T17:04:19Z")
> 
> ObjectId("5c0805134b62a2bd736fe79d").str
5c0805134b62a2bd736fe79d
> 
> 
> 
> ObjectId("507c7f79bcf86cd7994f6c0e").toString()
ObjectId("507c7f79bcf86cd7994f6c0e")
> 
> typeof ObjectId("507c7f79bcf86cd7994f6c0e").toString()
string
> 
> 
> ObjectId("507c7f79bcf86cd7994f6c0e").valueOf()
507c7f79bcf86cd7994f6c0e
> 
> 
> typeof ObjectId("507c7f79bcf86cd7994f6c0e").valueOf()
string
> 

UUID

Generates a BSON UUID object.

UUID() has the following syntax:

Parameter Type Description
hex string

Optional. Specify a 36 character string to convert to a UUID BSON object. If not provided, MongoDB generates a random UUID in RFC 4122 v4 format.

Changed in version 3.6: In earlier versions of the mongo shell, uuid required a hexadecimal string argument. 

> UUID()
UUID("c2a3e54d-fb50-4b00-b6d0-2c7b36adcd5d")
> 
> 
> var myuuid = '3b241101-e2bb-4255-8caf-4136c566a962'
> 
> UUID(myuuid)
UUID("3b241101-e2bb-4255-8caf-4136c566a962")
> 
> UUID("dee11d4e-63c6-4d90-983c-5c9f1e79e96c")
UUID("dee11d4e-63c6-4d90-983c-5c9f1e79e96c")
>
> for(i=1;i<6;i++){db.mycol.insert({_id:ObjectId(),name:'mongodb',pin: 10000+i})}
WriteResult({ "nInserted" : 1 })
> 
> for(i=1;i<6;i++){db.mycol.insert({_id:UUID(),name:'mongodb',pin: 10000+i})}
WriteResult({ "nInserted" : 1 })
> 
> db.mycol.find()
{ "_id" : ObjectId("5c0812a74b62a2bd736fef73"), "name" : "mongodb", "pin" : 10001 }
{ "_id" : ObjectId("5c0812a74b62a2bd736fef74"), "name" : "mongodb", "pin" : 10002 }
{ "_id" : ObjectId("5c0812a74b62a2bd736fef75"), "name" : "mongodb", "pin" : 10003 }
{ "_id" : ObjectId("5c0812a74b62a2bd736fef76"), "name" : "mongodb", "pin" : 10004 }
{ "_id" : ObjectId("5c0812a74b62a2bd736fef77"), "name" : "mongodb", "pin" : 10005 }
{ "_id" : UUID("6c58c69c-3cfd-4ab7-8db6-315c0a576d0f"), "name" : "mongodb", "pin" : 10001 }
{ "_id" : UUID("7469cde1-245b-43c1-824e-c554a5d10713"), "name" : "mongodb", "pin" : 10002 }
{ "_id" : UUID("fb069844-8edb-4008-88a7-b4e61de2242c"), "name" : "mongodb", "pin" : 10003 }
{ "_id" : UUID("3975d7a6-007b-4089-ba0d-8f30bfd11e66"), "name" : "mongodb", "pin" : 10004 }
{ "_id" : UUID("04234031-8090-4d82-bf9a-d71a9f7ae392"), "name" : "mongodb", "pin" : 10005 }
> 
> 
> 


  • Ask Question