mongo-db
  1. mongo-db-data-types

Data Types - (MongoDB Basics)

In MongoDB, each document stored in a collection can have different key-value pairs, each value can have a different data type. In this page, you will learn about different data types in MongoDB.

Syntax

MongoDB provides different data types such as:

  • string
  • integer
  • double
  • boolean
  • arrays
  • timestamp
  • date
  • ObjectId
  • null

Example

{
   "_id": ObjectId("67d98f5152e223133f430c22"),
   "name": "John",
   "age": 28,
   "address": {
                  "street": "101 Main St",
                  "city": "Los Angeles",
                  "state": "CA",
                  "zip": "90001"
              },
   "interests": ["music", "baseball"],
   "date_created": ISODate("2022-01-01T00:00:00Z"),
   "isMarried": true,
   "salary": 50000.59,
   "notes": null
}

In this example, you can see different data types used to represent data in MongoDB. The _id field uses the ObjectId data type, the name field uses the string data type, the age field uses the integer data type, the address field uses the object data type, the date_created field uses the date data type, the isMarried field uses the boolean data type, the salary field uses the double data type, and the notes field uses the null data type.

Output

The output of data types in MongoDB is similar to the format in which it is stored in the database. When you query the database using the MongoDB shell or a programming language, the data is returned in the same format as it is stored.

Explanation

MongoDB supports many data types that are commonly used in programming. Each data type has its own specific use case and functionalities in MongoDB.

Use

It is important to understand the different data types in MongoDB as it can help you store data efficiently and accurately. Using the correct data type for the data you are storing can make querying and manipulating the data more efficient.

Important Points

  • MongoDB supports many data types such as string, integer, double, boolean, arrays, timestamp, date, ObjectId, and null.
  • MongoDB uses dynamic schema where each document can have different key-value pairs with different data types.

Summary

In this page, we discussed different data types in MongoDB. We covered the syntax, example, output, explanation, use, and important points of data types. Understanding and using the right data types can help you store, query, and manipulate data more efficiently in MongoDB.

Published on: