This website uses cookies to enhance the user experience

Data Types

Share:

MongoDB, one of the most popular NoSQL databases today, is designed to store, process and return data in a structured and meaningful way. Before diving deep into working with MongoDB, it's crucial to familiarize yourself with the different data types that MongoDB supports. This chapter will introduce you to each data type, along with code snippets to offer you a hands-on learning experience.

It’s important to note that MongoDB uses BSON (Binary JavaScript Object Notation), a binary representation of JSON-like documents. BSON supports more data types than JSON, allowing for a rich data representation in documents.

  1. String:
    The string data type is used to store data as text. You should know that MongoDB stores data in UTF-8 format, this ensures a robust system handling different data entries from multiple languages effectively.
var empDetails = {
"name" : "Sam",
"profession" : "Engineer"
}

In this example, "Sam" and "Engineer" are strings.

  1. Integer:
    The integer data type is used to store a numerical value. MongoDB provides support for integer with 32 bit and 64 bit, depending on the server one is using.
var empDetails = {
"age" : 25,
"experience" : 2
}

Here, 25 and 2 are integers.

  1. Boolean:
    Boolean values are data types that have one of two possible values, either true or false.
var empDetails = {
"is_active" : true,
"is_retired" : false
}

In this example, "is_active" and "is_retired" are boolean types.

  1. Double:
    This data type is used to store floating point values, i.e., numbers that have a fractional component in them.
var empDetails = {
"weight" : 75.5,
"height" : 180.20
}

Here, 75.5 and 180.20 are double data types.

  1. Arrays:
    Arrays in MongoDB are used to store multiple values in a single key.
var empDetails = {
"skills" : ["java", "python", "c++"]
}

In this example, "skills" is an array holding three strings.

  1. Object:
    The object data type is used for embedded documents.
var empDetails = {
"address" : {
                "street" : "5th avenue",
                "city"   : "NY",
                "country": "USA"
              }
}

Here, "address" is an object which consist different key-value pairs.

  1. Null:
    This type is used to store a Null value.
var empDetails = {
"middle_name" : null
}

In this case, "middle_name" is null.

  1. Timestamp:
    This data type is used to store the time of creation of a document.

  2. Date:
    This data type is used for storing the current date or time in UNIX time format. You can create a date object by using the new Date() command.

var empDetails = {
"join_date" : new Date()
}

In this example, "join_date" is storing the current date and time.

  1. Object ID:
    Object ID is a data type which is typically used in the _id field. Each document stored in MongoDB will have a unique _id. It's a 12-byte identifier typically generated by the MongoDB driver and it consists of:
  • A 4-byte timestamp, showing creation of ObjectId, measured in seconds.
  • A 5-byte random value, unique to the machine and process.
  • A 3-byte incrementing counter, initialized to a random value.
var empDetails = {
"_id" : ObjectId()
}

In this case, _id is an Object ID.

Furthermore, MongoDB also supports Binary and Code data types. The binary data type is used to store binary data, while the code data type is used to store JavaScript code into the document.

By becoming familiar with each data type, you can ensure that you're using the most appropriate one for your data, which improves the efficiency and functionality of your MongoDB operations. Do remember, understanding data types is one of the core principles of working with any database, including MongoDB.

So, ensure you have a solid understanding of these MongoDB data types and start querying with confidence!

0 Comment


Sign up or Log in to leave a comment


Recent job openings

Cyprus

Remote

Full-time

Java

Java

JavaScript

JavaScript

+14

posted 2 days ago

France, Grenoble, Auvergne-Rhône-Alpes

Remote

Full-time

Python

Python

Java

Java

posted 2 days ago

Croatia, Zagreb, Grad Zagreb

Remote

Full-time

Python

Python

Bash

Bash

+12

posted 2 days ago

United States, El Dorado Hills, CA

Remote

Full-time

Java

Java

JavaScript

JavaScript

+7

posted 2 days ago
company logo
Software Trainee

Techmaxsoft

India, Nagpur, MH

Remote

Java

Java

JavaScript

JavaScript

+6

posted 2 days ago