Read it in 7 Mins
MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases, MongoDB makes use of collections and documents.
You can read more about MongoDB here.
An example of a MongoDB document is shown below:
{ _id: ObjectId(7df78ad8902ce46d) title: 'Awesome Post', description: 'This is an awesome post', tags: ['tours', 'photography'], likes: 100, comments: [ { user:'user1', message: 'My first comment', dateCreated: new Date(2011,1,20,2,15), like: 0 }, { user:'user2', message: 'My second comments', dateCreated: new Date(2011,1,25,7,45), like: 5 } ] }
_id is a 12 bytes hexadecimal number which assures the uniqueness of every document. It is called ObjectId.
In this blog we are going to learn about MongoDB, ObjectId and how to generate it manually.
Data Representation in JSON or BSON. An example of a JSON object is given below.
{ "name" : "Carlos Smith", "title" : "Product Manager", "location" : "New York, NY", "twitter" : "@MongoDB", "facebook" : "@MongoDB" }
You can read more about the use cases of MongoDB here.
The major and the key differences between MySQL and MongoDB are listed here:
MongoDB is an open-source database developed by MongoDB, Inc. MongoDB stores data in JSON-like documents that can vary in structure. It is a popular NoSQL database.
MySQL is a popular open-source relational database management system (RDBMS) that is developed, distributed and supported by Oracle Corporation. MySQL uses tables to store the data.You can read more about the differences here.
Let's go through the installation separately for Windows and Linux operating systems.
Link to download MongoDB.
You can unzip and run the file to install MongoDB for Windows.
Now, run the below command inside the command prompt, powershell or terminal to start MongoDB shell where we can create the Object IDs for this blog.
It's important to know that MongoDB uses BSON format to store data.
As we have already seen how JSON looks, now let's take a look at BSON and understand the differences between JSON and BSON.
BSON stands for binary JSON (a superset of JSON with some more data types, most importantly binary byte array).
You can read more about the differences here.
Note: Object ID is a unique identifier for each record which is created by declaring ObjectId as a method, as you will now see. Do not get confused between Object ID and ObjectId.
To create a new Object ID manually within the MongoDB you can declare ObjectId as a method. In the below image you can observe that we are declaring a variable having ObjectId method as a value. It will return a unique hexadecimal which we can store in a variable named myObjectId.
In this example, the value of myObjectId would be: ObjectId("6009cb85e65f6dce28fb3e51").
In the above image, you can observe that each time it is returning a unique hexadecimal value.
Sometimes you want to define your own unique hexadecimal value, and MongoDB allows you to perform this action. In the above example the hexadecimal value is generated.
In this scenario, we will define an object ID with a hexadecimal value as a parameter of the ObjectId method.
In this example, the value of newObjectId would be: ObjectId("507f1f77bcf86cd799439011").
In the above examples we are unable to get the hexadecimal string, as it will return you the whole method having the unique hexadecimal value.
To extract the unique hexadecimal as a string from the Object ID, we need to use ‘str’ attribute which is available in ObjectId method.
In this example, the value of the ObjectId("507f191e810c19729de860ea").str method returned the hexadecimal string which is inside ObjectId method.
This will return the creation time of this document in ISO date format.
This will return the ObjectId(<hexadecimal>) of this document in string format.
Note that the returned values of newObjectId.valueOf() and newObjectId.str are the same.
MongoDB is a NoSQL DB that is widely used in the industry, and also easy to learn. Good luck!
28 Jul 2022
25 Jul 2022
25 Jul 2022
25 Jul 2022
22 Jul 2022
22 Jul 2022