Data Types in MongoDB
Review the data types available to you when storing data in this document store.
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:
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
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.
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. 5. 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.
Date() 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.
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.
Published at DZone with permission of Anoop Kumar Sharma, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.