ObjectId in MongoDB
You have seen the use of MongoDB ObjectId in previous chapters. In this chapter, we will understand the structure of ObjectId.
An ObjectId is a BSON type (12 bytes) with the following structure:
The first 4 bytes represent the number of seconds from UNIX Epoch.
The next 3 bytes is the machine id .
The next 2 bytes are the process id .
And the last 3 bytes are a random counting value .
MongoDB uses ObjectId as the default value of the _id field for each Document that is created while creating any Document. The complex combination of ObjectId makes all _id fields unique.
Create a new ObjectId in MongoDB
To create a new ObjectId, you use:
> newObjectId = ObjectId ()
The above command returns the following unique created id:
ObjectId ( "5349b4ddd2781d08c09890f3" )
In MongoDB, instead of creating ObjectId, you can also provide an ID of 12 bytes as follows:
> myObjectId = ObjectId ( "5349b4ddd2781d08c09890f4" )
Get the Timestamp of a Document
By default, _id ObjectId stores a Timestamp of 4 bytes in length, so in most cases, you do not need to store the creation time of any Document. You can get the creation time of a Document using the getTimestamp method:
> ObjectId ( "5349b4ddd2781d08c09890f4" ). getTimestamp ()
This command will return the creation time of this Document in ISO Date format:
ISODate ( "2014-04-12T21:49:17Z" )
Convert ObjectId into String in MongoDB
In some cases, you may need the value of ObjectId in string format. To convert ObjectId into a string, you use:
> newObjectId . str
The above code will return Guid's string format:
5349b4ddd2781d08c09890f3
According to Tutorialspoint
Previous article: Limitations of index in MongoDB
Next post: Map Reduce in MongoDB
You should read it
May be interested
- Replica Set in MongoDBreplication is the process of synchronizing data from multiple servers. replication provides redundancy and increased data availability for multiple data copies on many different database servers. replication protects a database from the loss of a particular server. replication also allows you to recover data from hardware errors or service disconnections. with additional data copies, you can use it for recovery, reporting, or backup.
- Shard in MongoDBsharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
- Create Backup in MongoDBto create a database backup in mongodb, you should use the mongodump command. this command will dump all server data into dump directory. there are many options available from which you can limit the amount of data or backup created by remote server.
- MongoDB Deploymentwhen you are preparing a mongodb deployment, you should understand how your application is being supported in production.
- Relationship in MongoDBrelationship in mongodb represents how the documents are related. relationship can be modeled through the embeded and referenced method.
- Reference Database in MongoDBas shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.