Search⌘ K
AI Features

MongoDB Data Types

Explore MongoDB data types used to store various forms of data within BSON documents. Learn how to work with strings, integers, decimals, arrays, ObjectId, dates, binary data, and regular expressions to manage your database content effectively. Understand the role of each type and commands to insert and query these data types.

MongoDB stores documents in binary serialized format called BSON. Let’s go through each BSON type supported by MongoDB.

The insert and find 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.

Markdown
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,6482,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,2954,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.

Markdown
db.tasks.insertOne({
progress: 99.99
});

Boolean

A boolean field supports only two values, true and false.

Markdown
db.tasks.insertOne({
name: "Setup ToDo App",
is_public: true,
});

Next, we fetch the documents of the users ...