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

5 ★ | 1 Vote

May be interested

  • Text Search in MongoDBText Search in MongoDB
    starting with version 2.4, mongodb started supporting text indexes to search within the string content.
  • Shard in MongoDBShard in MongoDB
    sharding 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.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Map Reduce in MongoDBMap Reduce in MongoDB
    in mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
  • Aggregation in MongoDBAggregation in MongoDB
    aggregation can be understood as aggregation. the aggregation operation handles data records and returns calculated results. the operations group the values ​​from multiple documents together, and can perform multiple operations on the grouped data to return a single result. in sql, count (*) and group by are equivalent to aggregation in mongodb.
  • Data modeling in MongoDBData modeling in MongoDB
    data in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
  • How to use Aggregation Pipeline in MongoDBHow to use Aggregation Pipeline in MongoDB
    if you are using mongodb's mapreduce, it is best to switch to aggregation pipeline for more efficient computation.
  • GridFS in MongoDBGridFS in MongoDB
    gridfs is the mongodb specification for storing and collecting large files such as images, audio, video files, etc. it is a type of file system to store files but its data is stored inside mongodb collections. .
  • Query analysis in MongoDBQuery analysis in MongoDB
    analyzing queries is a very important aspect to assess the effectiveness of database and the effectiveness of the designed index. we will explore the two most frequently used queries, $ explain and $ hint.