TopD Learning

MongoDB Blog Banner

What is MongoDB? What are the different data types in MongoDB?

Do you want to learn about MongoDB? In this blog, we will explain what MongoDB is and the different data types that you can use with MongoDB. If you are new to MongoDB, then this blog is for you! Read more…

Introduction

A database is used for storing data. Most companies need to store a lot of data. MongoDB is one of the most popular NoSQL databases used by businesses.

It is a document-oriented database that allows us to use JavaScript as our language of choice when working with this database. This blog will take a look at what MongoDB is, what are the different features of MongoDB and what are it’s common data types with examples. .

Table of Content

  1. What is MongoDB?
  2. What are the features of MongoDB?
  3. Common Data Types In MongoDB

What is MongoDB?

MongoDB is a document-oriented NoSQL database that is used for high volume data storage. Instead of using tables and rows like traditional relational databases, MongoDB uses collections and documents. 

Documents are made up of key-value pairs, which are the basic unit of data in MongoDB. Collections contain sets of documents and function as the equivalent of relational database tables. MongoDB is a database that emerged in the mid-2000s.

What are the features of MongoDB?

Listed below are some of the features of MongoDB:

  • Each database contains collections which in turn contains documents. Each document can be different with a varying number of fields. The size and content of each document can be different from each other.
  • The document structure is more in line with how developers construct their classes and objects in their respective programming languages. Developers will often say that their classes are not rows and columns but have a clear structure with key-value pairs.
  • The rows (or documents as called in MongoDB) doesn’t need to have a schema defined beforehand. Instead, the fields can be created on the fly.
  • The data model available within MongoDB allows you to represent hierarchical relationships, to store arrays, and other more complex structures more easily.
  • Scalability – The MongoDB environments are very scalable. Companies across the world have defined clusters with some of them running 100+ nodes with around millions of documents within the database

Common Data Types In MongoDB

MongoDB is an excellent choice for a database due to its scalability, availability, and performance. One of the reasons MongoDB is so efficient is because of its data types – which allow users to store data more compactly while still being able to perform very powerful queries.

The most commonly used data types in MongoDB are:

  1. String:

    Strings are one of the most basic and widely used data types. They are used to represent text and are encoded in UTF-8, which allows for most international characters. BSON strings are a great way to store text data.

    Example:

{

“user_name”:  “Bill Gates”,

“user_skills”:  “Software Development”,

“user_salary”:  5500,

“user_status”:  true,

}

  1. Integer

    Numeric values are stored using the integer data type. Depending on the server, it can store either 32-bit or 64-bit numbers.

    Example:

{

“user_name”:  “Bill Gates”,

“user_skills”:  “Software Development”,

“user_salary”:  5500,

“user_status”:  true,

}

user_salary is of the type integer since it stores a numeric number.

  1. Double

    Numeric numbers containing 8 bytes (64-bit IEEE 754 floating point) floating-point are stored using the double data type.

    Example:

{

“user_name”:  “Bill Gates”,

“user_skills”:  “Software Development”,

“user_score”:  98.51,

“user_status”:  true,

}

  1. Boolean

    The boolean data type is used for storing values that are either true or false. In the example below, the field user_status is of the type boolean because it stores the value true.

    Booleans take up less space than integers or strings and avoid unwanted comparison side effects.

    Example:

{

“user_name”:  “Bill Gates”,

“user_skills”:  “Software Development”,

“user_score”:  98.51,

“user_status”:  true,

}

  1. Array

    The array is stored using the array data type. We can store several values in a single key of the document with an array data type.


Example:

{

“user_name”:  “Bill Gates”,

“user_skills”:  [“Software Development”, ”Java”, “Python”],

“user_score”:  98.51,

“user_status”:  true,

}

  1. Object

    An embedded document is a key-value pair that is put inside another document. Embedded documents are stored using the object data type. 


Example:
{

“product_code”: “0000-XYZ”,

“product_price”: 39.99,

“product_dimensions”: {

“product _height”: 1000,

“product_width”: 90,

“product_depth”: 600,

},

“product_availability”: true,

}

Because it has its own set of key-value pairs, the product dimensions field in the example above is an embedded document. As a result, this field is an Object field.

  1. Date

    The current date or time is stored in the ‘Date’ data type. The returning date can be done in a variety of ways; either a string or a date object. There are three strategies that can be used in this situation. 

    a) The Date() function returns a string. 
    b) Return a date object with New Date().
    c) ISODate() returns a date object as well.


Example:

{

“employee_name”: “John Wayne”,

“employee_dob”: ISODate(“2001-02-10T12:45:22.389Z”),

“employee_marks”: 83.40

}

  1. Timestamp

    The term “Timestamp” refers to a set of characters used to describe the date and time of an occurrence. The timestamp data type is commonly used to track the creation, editing, and updating of documents.

    Such characters are stored in the timestamp data type. To construct a timestamp, use the new Timestamp().

Example

{

“product_code”: “0000-XYZ”,

“product_price”: 39.99,

“product_created”: Timestamp(1531456567, 1),

“product_availability”: true,

}

  1. Null

    The NULL data type is used to represent a value of zero or no value, as the name implies.

When a null value field in a document is queried, this is what it looks like:


Example:


{

“product_code”: “0000-XYZ”,

“product_price”: 39.99,

“product_color”: null,

“product_availability”: true,

}

  1. Binary

    This data type is used in fields to hold binary data. In a relational database management system, this data type corresponds to the Blob type. Nonetheless, on the grounds that MongoDB has a report size limitation of 16MB, binary data can be implanted inside a document utilizing the Binary data type in the event that the full size of the binary data in addition to different fields is under 16MB.

Example:

{

“_id”: “4567rttyygjuhutfv7890”,

“product_code”: “1234-ABCD”,

“product_price”: 49.99,

“product_availability”: true,

“product_picture”:BinData(1, “rk56tyvbu5677ghugf456…”),

}

Conclusion

We hope this article has helped you understand what MongoDB is and what its common data types are. 

TopD Learning is a great place for students and professionals alike as they are able to gain skills that provide them a competitive edge.

Gain the edge and learn the latest technologies at TopD Learning.

If you have any questions about this blog, about MongoDB or any other technology, please contact us and we would be happy to help.

Leave a Comment

Your email address will not be published. Required fields are marked *

Learning Mode: Instructor LED Training

AWS Solution Architect Certification Training Course