Nearby lessons

5 of 22

📘 MongoDB: Data Types

MongoDB supports a variety of data types to store different kinds of information in documents. These data types help manage text, numbers, arrays, objects, dates, and more.

Common MongoDB Data Types

Example01
MongoDB Shell
NoSQL
Database
1
2String – Textual data
3Double – Floating-point numbers
432-bit Integer – int32
564-bit Integer – int64
6Boolean – true or false
7Array – List of values
8Object – Embedded document
9Null – Represents null values
10Regular Expression – Pattern matching
11Timestamp – High precision time
12Date – Date and time
13ObjectId – Unique identifier
14
Query Result
JSON Output
These are the basic data types used to structure MongoDB documents.

Example document using multiple data types

Example02
MongoDB Shell
NoSQL
Database
1{
2 "_id": ObjectId("507f1f77bcf86cd799439011"),
3 "name": "Yahoo Baba",
4 "age": 25,
5 "married": false,
6 "dob": ISODate("2000-10-15T08:00:00Z"),
7 "weight": 72.50,
8 "kids": null,
9 "hobbies": ["music", "sports"],
10 "address": {
11 "street": "123 Main St",
12 "city": "New York",
13 "zip": 10001
14 }
15}
Query Result
JSON Output
Demonstrates multiple MongoDB data types such as String, Number, Boolean, Null, Array, Object, and Date.

Example of MongoDB Date Data Type (UTC Time)

Example03
MongoDB Shell
NoSQL
Database
1{ dob: ISODate("2024-10-18T18:30:00Z") }
Query Result
JSON Output
Represents UTC (Coordinated Universal Time).

Example of Central European Time

Example04
MongoDB Shell
NoSQL
Database
1{ dob: ISODate("2024-10-18T18:30:00+02:00") }
Query Result
JSON Output
Represents CET (Central European Time) with a +2:00 offset.

Example of Eastern Standard Time

Example05
MongoDB Shell
NoSQL
Database
1{ dob: ISODate("2024-10-18T18:30:00-05:00") }
Query Result
JSON Output
Represents EST (Eastern Standard Time) with a -5:00 offset.

Example using Local Time Zone

Example06
MongoDB Shell
NoSQL
Database
1{ dob: ISODate("2024-10-18T18:30:00") }
Query Result
JSON Output
Represents date in the local system time zone.

Example using new Date() for current time

Example07
MongoDB Shell
NoSQL
Database
1{ dob: new Date() }
Query Result
JSON Output
Automatically inserts the current date and time.

Create a collection named 'personal'

Example08
MongoDB Shell
NoSQL
Database
1db.createCollection("personal")
Query Result
JSON Output
{ ok: 1 }

Insert example document

Example09
MongoDB Shell
NoSQL
Database
1db.personal.insertOne({
2 name: "Rajesh Rathwa",
3 age: 20,
4 married: false,
5 dob: ISODate("2000-10-15T08:00:00Z"),
6 weight: 72.50,
7 kids: null,
8 hobbies: ["music", "sports"],
9 address: {
10 street: "123 main st",
11 city: "Delhi",
12 zip: 390022
13 }
14});
Query Result
JSON Output
{
acknowledged: true,
insertedId: ObjectId('672b4f08f5502a92ff0d8197')
}

Insert another document

Example10
MongoDB Shell
NoSQL
Database
1db.personal.insertOne({
2 name: "Raj Rathwa",
3 age: 25,
4 married: true,
5 dob: ISODate("2000-10-15T08:00:00Z"),
6 weight: 70.50,
7 kids: null,
8 hobbies: ["music", "travel"],
9 address: {
10 street: "13 main st",
11 city: "Goa",
12 zip: 380022
13 }
14});
Query Result
JSON Output
{
acknowledged: true,
insertedId: ObjectId('672b504ff5502a92ff0d8198')
}

Insert document with current date

Example11
MongoDB Shell
NoSQL
Database
1db.personal.insertOne({
2 name: "Raj Rathwa",
3 age: 25,
4 married: true,
5 dob: new Date(),
6 weight: 70.50,
7 kids: null,
8 hobbies: ["music", "travel"],
9 address: {
10 street: "13 main st",
11 city: "Goa",
12 zip: 380022
13 }
14});
Query Result
JSON Output
{
acknowledged: true,
insertedId: ObjectId('672b511af5502a92ff0d8199')
}

View all documents in the 'personal' collection

Example12
MongoDB Shell
NoSQL
Database
1db.personal.find()
Query Result
JSON Output
[
{
_id: ObjectId('672b4f08f5502a92ff0d8197'),
name: 'Rajesh Rathwa',
age: 20,
married: false,
dob: ISODate('2000-10-15T08:00:00.000Z'),
weight: 72.5,
kids: null,
hobbies: ['music', 'sports'],
address: { street: '123 main st', city: 'Delhi', zip: 390022 }
},
{
_id: ObjectId('672b504ff5502a92ff0d8198'),
name: 'Raj Rathwa',
age: 25,
married: true,
dob: ISODate('2000-10-15T08:00:00.000Z'),
weight: 70.5,
kids: null,
hobbies: ['music', 'travel'],
address: { street: '13 main st', city: 'Goa', zip: 380022 }
},
{
_id: ObjectId('672b511af5502a92ff0d8199'),
name: 'Raj Rathwa',
age: 25,
married: true,
dob: ISODate('2024-11-06T11:20:58.510Z'),
weight: 70.5,
kids: null,
hobbies: ['music', 'travel'],
address: { street: '13 main st', city: 'Goa', zip: 380022 }
}
]