DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Data Types in MongoDB

Data Types in MongoDB

Review the data types available to you when storing data in this document store.

Anoop Kumar Sharma user avatar by
Anoop Kumar Sharma
CORE ·
Nov. 21, 18 · Tutorial
Like (4)
Save
Tweet
Share
37.93K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will learn about the data types and their usage in MongoDB.  If you are new to MongoDB then I would recommend you to go through the previous article of the series:

  • Part 1 — Getting started with MongoDB
  • Part 2 — Configure a Windows Service for MongoDB
  • Part 3 — Working with Database in MongoDB

As we already know, MongoDB stores data in BSON format. BSON stands for the Binary-encoded format of JSON. Using BSON, we can make remote procedure calls in MongoDB. In MongoDB, each data type has an alias as well as a number which can be used for finding or searching the record in MongoDB. We will also learn about How to use that number/alias in searching in this article. We will use find () method which is used to select the document in the collection and is similar to the “Select” keyword used in SQL and another method is pretty () which makes easier to read the output of the document. Data types in MongoDB are mentioned below:

Datatypes in MongoDB

Let’s try some of the popular data types of the MongoDB:

1. Double: Double data type is used to store floating values. In the below example, we have inserted the double data type (floating value) in the collection

Double Datatype

2. String: MongoDB stores BSON Strings in UTF-8 format. Drivers for each programming language converts the language’s string to UTF-8 when serializing and deserializing.

Strings3. Object: Object data type is used for embedding the documents. Embedded Document are the documents embedded in another document in the form of a Key-Value pair.

Object Datatype 4. Array: Array data type is used to store the array. With an array data type, we can store multiple values in a single key of the document. Array Datatype5. ObjectId: ObjectId is likely unique, small, fast to generate. Its values are of 12 Bytes out of which first four Byte the s for timestamp (that reflects ObjectID creation in the seconds since the UNIX epoch), 5 bytes for a random value and 3 bytes counter, starting with a random value. In MongoDB, if a document is inserted without the _id field, Mongo will generate a unique _id that acts as a primary key for the document in the collection.

ObjectId6. Boolean: Boolean data type stores the Boolean values, i.e. true/false.

Boolean Datatype7. Null: Null data type is used to store null values (also for non-existent fields) in it.

Null Datatype8. Date: Date data type is used to store in date or time in the Unix time format.

Date DatatypeDate() returns Date as a string. new Date() and ISODate() returns Date object wrapped in ISODate() wrapper. With the help of getMonth() method, we can get the month for the Date object. Months are zero indexed so in the case of October, we are getting 10 as the value.

9. Timestamp: Timestamp values are 64-bit value out of which first 32 bits the are time_t value (seconds since the UNIX epoch) and the second 32 bits are incremental ordinal for operations within a given second. For single, Instance of the mongoD, timestamp values are always unique.

Timestamp values10. Integer: In MongoDB, Integer can be 32 and 64-bit depending upon the server.

Integer in MongoDBAs we said, Alias or Number mentioned in the data type table is useful in the filtering the documents of the collection. Let’s try to search records with alias/number provided by the MongoDB.

Alias/Number provided by MongoDB

In above example, we have added Name and Code in Employee collection. In First record and third record, we have added code with string data type and in the second record, the code is for an integer type. Firstly, we check the data in the Employee collection with the find() method. $type selects the documents where the value of the field is an instance of the specified BSON type(s). Querying by data type is useful when dealing with highly unstructured data where data types are not predictable. The same thing happens with the Code key in the Employee collection. Pass the value as number or alias in order to filter the documents in the collection. In above example, we filtered the data which have Code as string data type.

Hope this will help you. Thanks.



If you enjoyed this article and want to learn more about MongoDB, check out this collection of tutorials and articles on all things MongoDB.

Data (computing) MongoDB Data Types Database Document

Published at DZone with permission of Anoop Kumar Sharma, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • The Quest for REST
  • How To Create and Edit Excel XLSX Documents in Java
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: