Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Data Types in MongoDB

DZone's Guide to

Data Types in MongoDB

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

· Database Zone ·
Free Resource

MariaDB TX, proven in production and driven by the community, is a complete database solution for any and every enterprise — a modern database for modern applications.

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:

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.

MariaDB AX is an open source database for modern analytics: distributed, columnar and easy to use.

Topics:
database ,mongodb ,data types ,document store ,datatypes in mongodb ,tutorial ,bson

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}