Contents of page >
1) What is ObjectId in MongoDB?
ObjectId is of 12-byte hexadecimal string.
This is what ObjectId looks like > ObjectId(“hexadecimal”)
Example > ObjectId("584ebed11127dea5ece72742")
Let’s see what exactly ObjectId consists of >
- First 4-byte - represents the seconds since the Unix epoch,
- Next 3-byte - represents machine identifier,
- Next 2-byte - represents process id, and
- Next 3-byte - represents random value.
2) Inserting in collection in MongoDB >
> db.employee.insert({ firstName:"ankit"})
|
Above line will create collection (if collection already exists it will insert records in it).
Let's query collection >
> db.employee.find();
|
Output>
{ "_id" : ObjectId("588341b303b0c717da77d641"), "firstName" : "ankit" }
|
We can see that field _id is formed automatically. Its value is ObjectId("588341b303b0c717da77d641")
3) Creating new ObjectId() in MongoDB>
Method ObjectId() - Returns the hexadecimal string representation of the object.
To generate a new ObjectId, Simply type ObjectId() with no argument and execute it
obj = ObjectId()
|
Output>
So, value of obj is -
ObjectId("588342aa03b0c717da77d642")
|
4) Creating custom/own ObjectId() in MongoDB>
To generate a ObjectId of your choice, Simply type ObjectId(“HexadecimalString”) with hexadecimal string of 12 bytes.
But, we need to ensure that HexadecimalString is unique otherwise error - ‘errmsg" : "E11000 duplicate key error index’ will be thrown.
myObj = ObjectId("123456aa01b0c234da56d789")
|
Output>
So, value of myObj is -
ObjectId("123456aa01b0c234da56d789")
|
5) Now, Insert own objectId in collection in MongoDB >
> db.employee.insert({ _id :ObjectId("123341b303b0c717da77d123"),firstName:"ankit"})
|
6) How to find ObjectId was created at what time in MongoDB?
As we read above that first 4-byte - represents the seconds since the Unix epoch,
Use getTimestamp() method.
time = ObjectId("588341b303b0c717da77d641").getTimestamp()
|
Output>
So, value of time is -
ISODate("2017-01-21T11:10:43Z")
|
7) How to convert ObjectId to string in MongoDB?
Use str
x = ObjectId("588341b303b0c717da77d641").str
|
Output>
So, value of x is -
588341b303b0c717da77d641
|
8) SUMMARY of ObjectId in MongoDB>
So in this mongoDB tutorial we learned about objectId in MongoDB.
1) What is ObjectId in MongoDB?
ObjectId is of 12-byte hexadecimal string.
Example of > ObjectId("584ebed11127dea5ece72742")
- First 4-byte - represents the seconds since the Unix epoch,
- Next 3-byte - represents machine identifier,
- Next 2-byte - represents process id, and
- Next 3-byte - represents random value.
2) Inserting in collection in MongoDB >
> db.employee.insert({ firstName:"ankit"})
|
Let's query collection >
> db.employee.find();
{ "_id" : ObjectId("588341b303b0c717da77d641"), "firstName" : "ankit" }
|
3) Creating new ObjectId() in MongoDB>
obj = ObjectId()
|
4) Creating custom/own ObjectId() in MongoDB>
myObj = ObjectId("123456aa01b0c234da56d789")
|
5) Now, Insert own objectId in collection in MongoDB >
db.employee.insert({ _id :ObjectId("123341b303b0c717da77d123"),firstName:"ankit"})
|
6) How to find ObjectId was created at what time in MongoDB?
Use getTimestamp() method.
time = ObjectId("588341b303b0c717da77d641").getTimestamp()
|
7) How to convert ObjectId to string in MongoDB?
x = ObjectId("588341b303b0c717da77d641").str
|
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
What is MongoDB - A quick introduction to database
Create new database in mongoDB
How to Drop collection in MongoDB
Create new collection(table) in mongoDB
Labels:
MongoDB