MongoDB Data Types
Learn about all the MongoDB data types and practice creating data type values.
MongoDB stores documents in binary serialized format called BSON. Let’s go through each BSON type supported by MongoDB.
The
insert
andfind
commands are used to provide an overview of data types. We’ll cover these in detail later on.
String
BSON strings are UTF-8, which allows us to use most international characters conveniently. In the below example, name
is a string field.
db.tasks.insertOne({name: "Setup ToDo App",});
Integer
The 32-bit integer type represents whole-number values. An integer field supports values in the range, -2,147,483,648
–2,147,483,648
.
Long
The 64-bit long integer type can represent larger whole numbers. A long integer field supports values in the range -4,294,967,295
–4,294,967,295
.
Decimal
A decimal value can represent numbers with decimal places (non-whole numbers). This gives a higher level of accuracy so most financial applications use the decimal type to store numeric values.
Double
We use the double data type to store floating values. In the below example, weight
is a double field.
db.tasks.insertOne({progress: 99.99});
Boolean
A boolean field supports only two values, true
and false
.
db.tasks.insertOne({name: "Setup ToDo App",is_public: true,});
Next, we ...