MongoDB Data Types

MongoDB uses the BSON binary format. BSON (binary JSON), is a binary serialization format and used to store documents and make remote procedure calls in MongoDB. BSON provides the following data types. Each data type has a corresponding number that can be used with the type operator to query documents by BSON type.

Type Description Number
Double used to store floating point values. 1
String BSON strings are UTF-8. It is most commonly used datatype. 2
Object used for embedded documents. 3
Array used to store multiples of values and data types. 4
Binary data used to store the binary data. 5
Undefined used to store the undefined values. 6
Object id used to store the document's id. 7
Boolean used to store a boolean value. 8
Date used to store the current date or time in UNIX time format. 9
Null used to store a Null value. 10
Regular Expression used to store regular expression. 11
JavaScript used to store the javascript data without a scope. 13
Symbol It is similar to the string data type, and not supported by a shell. 14
JavaScript (with scope) It stores javascript data with a scope. 15
Timestamp used to store a timestamp. 17
Min key Min key compares the value of the lowest BSON element. 255
Max key Max key compares the value against the highest BSON element. 127



MongoDB Data types Examples

Double Data Type

The double data type represents real numbers that have a decimal point and can be written in both positive and negative sign.

> db.dbl.insert({"Double":839.32})
WriteResult({ "nInserted" : 1 })
> db.dbl.find()
{ "_id" : ObjectId("5d3fddbbcfee7d34ea58b51e"), "Double" : 839.32 }
>

String

Strings are sequences of characters and written within quotes. BSON strings are UTF-8. It is most commonly used data type.

> db.str.insert({"String":"Welcome to Etutorialspoint"});
WriteResult({ "nInserted" : 1 })
> db.str.find();
{ "_id" : ObjectId("5d3fe7b2cfee7d34ea58b520"), "String" : "Welcome to Etutorialspoint" }
>

Object

Object data type is used for embedded documents. In this, a document can contain entire documents embedded as values in a parent document.

> var marks = {phy: 80, chem: 66, maths: 90}
> db.students.insert({name:"Priska", class:8, results:marks});
WriteResult({ "nInserted" : 1 })
> db.students.find().pretty();
{
	"_id" : ObjectId("5d442048bc126770df0ef402"),
	"name" : "Priska",
	"class" : 8,
	"results" : {
		"phy" : 80,
		"chem" : 66,
		"maths" : 90
	}
}

Array

It is used to store multiples of values and data types.

> db.arr.insert({"Name":["Priska","Somya","Soy"]});
WriteResult({ "nInserted" : 1 })
> db.arr.find();
{ "_id" : ObjectId("5d3fea9ccfee7d34ea58b521"), "Name" : [ "Priska", "Somya", "Soy" ] }
>

Binary Data

Binary data type is used to store binary data. It is a string of arbitrary bytes. By using this, we can store non UTF-8 string in the database.

db.binaryCol.insert({val: BinData("")})

Undefined Data Type

It is used to store the undefined values.

> db.undf.insert({"Name": undefined});
WriteResult({ "nInserted" : 1 })
> db.undf.find();
{ "_id" : ObjectId("5d3fed41cfee7d34ea58b522"), "Name" : undefined }
>

Object Id

An object id is a 12-byte id and used to store the document's id. All documents have a primary key stored in the _id field. It is created automatically or we can manually create this using ObjectId() method.

> var objId = ObjectId()
> db.emp.insert({_id:objId, Name: "Priska", Department: "Engineer"});
WriteResult({ "nInserted" : 1 })
> db.emp.find()
{ "_id" : ObjectId("5d3feef7cfee7d34ea58b523"), "Name" : "Priska", "Department" : "Engineer" }
>

Boolean

The boolean data type is used to store a boolean value. It has only two possible values either TRUE or FALSE.

> db.hobby.insert({dance:true,music:false,reading:true});
WriteResult({ "nInserted" : 1 })
> db.hobby.find().pretty();
{
        "_id" : ObjectId("5d442489bc126770df0ef404"),
        "dance" : true,
        "music" : false,
        "reading" : true
}

Date

The Date data type is used to store the current date. There are some pre-defined methods available to return date - Date(), ISODate().

> var d1 = Date();
> var d2 = new ISODate();
> db.storeDate.insert({"Date1":d1,"Date2":d2});
WriteResult({ "nInserted" : 1 })
> db.storeDate.find();
{ "_id" : ObjectId("5d4017cf8c64b8781bbb1ea9"), "Date1" : "Tue Jul 30 2019 15:35:14 GMT+0530 (India Standard Time)", "Date2" : ISODate("2019-07-30T10:05:28.560Z") }
>

Null

It stores Null value.

> db.interest.insert({"balance":null});
WriteResult({ "nInserted" : 1 })
> db.interest.find();
{ "_id" : ObjectId("5d4019008c64b8781bbb1eaa"), "balance" : null }
>



Javascript (with scope)

It is used to store the Javascript data with a scope.

> db.js.insert({code:"var a1=10; function data(a1){ alert(a1);}",scope:{}});
WriteResult({ "nInserted" : 1 })
> db.js.find().pretty();
{
    "_id" : ObjectId("5d401b378c64b8781bbb1eab"),
    "code" : "var a1=10; function data(a1){ alert(a1);}",
    "scope" : {

    }
}
>

Timestamp

Timestamp values are a 64 bit value, in which first 32 bit are time_t value and second 32 bit are an incremental ordinal for activities within a given second.

> var t = new Timestamp();
> db.testDT.insert({ts: t});
WriteResult({ "nInserted" : 1 })
> db.testDT.find();
{ "_id" : ObjectId("5d4022228c64b8781bbb1eac"), "ts" : Timestamp(1564484130, 1) }
>

Min Key

Min key compares the value of the lowest BSON element. It is smaller than any other value in database and used for comparison operations.

Max Key

Max key compares the value against the highest BSON element. It is greater than any other value in database and used for comparison operations.

> db.marks.insert([{x:1},{x:21},{x:"PRISKA"},{x:50},{x:null},{x:MinKey},{x:MaxKey}])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 7,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
> db.marks.find().sort({x:1})
{ "_id" : ObjectId("5d402d758c64b8781bbb1eb6"), "x" : { "$minKey" : 1 } }
{ "_id" : ObjectId("5d402d758c64b8781bbb1eb5"), "x" : null }
{ "_id" : ObjectId("5d402d758c64b8781bbb1eb1"), "x" : 1 }
{ "_id" : ObjectId("5d402d758c64b8781bbb1eb2"), "x" : 21 }
{ "_id" : ObjectId("5d402d758c64b8781bbb1eb4"), "x" : 50 }
{ "_id" : ObjectId("5d402d758c64b8781bbb1eb3"), "x" : "PRISKA" }
{ "_id" : ObjectId("5d402d758c64b8781bbb1eb7"), "x" : { "$maxKey" : 1 } }
>






Read more articles


General Knowledge



Learn Popular Language