As we are handling data in different formats throughout the internet, we should be aware of the kind of value the data variable can hold and type of operations that can be performed on it. This is called its Data type.
Data Model on the other hand describes about the storage, structure of the data in a database and how it can be managed.

Common Datatypes in MongoDB:
Here, we can consider MongoDB used in a React Node project and lets discuss about the most common datatypes supported by MongoDB in detail.

ObjectId:

To ensure uniqueness among the documents, a default 12byte identifier is generated in MongoDB and stored as _id. To store this id, ObjectId datatype is used.

String:

String data type stores any text data.
{ “name” : “John” }

Boolean:

It stores true or false values.
{ “isActive” : true };

Integer:

Integer stores 32-bit(int) and 64-bit(long) integer types.
{ “age” : 40 };

Double:

It stores floating point values.
{ “price” : 20.56 };

Date:

Date values are stored as Date data type and in UTC format by default.
{ “createdAt” : “2019-02-18T19:29:22.381Z” };
Here ‘Z’ represents the UTC format(Coordinated Universal Time). If we need to use data in any other format other than UTC, we can convert to the expected format as follows:

This is a utility function to convert date from database which is in UTC to IST for using in the application.

Array:

It is used to store a list of values in a single field.
{ “names”: [ “abc”, “hi”, “hello” };

Object:

Object datatype holds embedded documents where document contain another document.
{
“address”: {
“street” : “second cross”,
“country”: “USA”,
}
}

Hence we can use these data types for our application that are supported in MongoDB. Inspite of its datatype, the data that we transmit over the application should be secured and authenticated to avoid vulnerabilities.

Leave a Reply