Unit 2 Part 1
Unit 2 Part 1
String data types are one of the most common data types
used in MongoDB. You might be aware that many of the
international languages use some special characters.
In order to save these characters, the MongoDB drivers
convert these characters to UTF-8 format during the
serialization and de-serialization process, thus, making it
possible to store most of these international characters in
the BSON strings and validating the international
characters to be stored in the database.
String Data types
Code 1
db.BPBOnlineBooksDataTypesCollection.insert({ "Data Type
Alias": "string", "Data Type Number": "2", "Data Type Value":
"BPB Publications – The Largest Online Resource for IT
Books“});
Code 2
db.BPBOnlineBooksDataTypesCollection.find({"Data Type Alias":
"string"}).pretty();
Double Data Type
Double data types are used to store the floating point values.
Floating point values have decimal points, which are somewhat
similar to integer values but they have decimal values with
them. For example, 777.27 or 12.10 are decimal type values
and they are called double types in MongoDB
db.BPBOnlineBooksDataTypesCollection.insert({"Data Type
Alias": "double", "Data Type Number": "1", "Data Type Value":
777.27});
db.BPBOnlineBooksDataTypesCollection.find({"Data Type Alias":
"double"}).pretty();
Array Data Type
The array data types are used to store the group of similar data
type values which are linked or associated in the form of key
and value.
In our example, we have defined 2 arrays and then we will insert
these arrays as a value and the code for the same is as follows:
var BPBBookStoresinIndia = ['New Delhi', 'Mumbai', 'Kolkata’,
'Chennai’];
var BPBBookStoresinUSA = ['New York', 'Atlanta', 'Arizona’, 'New
Jersey'];
Array Data Type
db.BPBOnlineBooksDataTypesCollection.insert({ "Data
Type Alias": "array", "Data Type Number": "4","Data Type
Value 1": BPBBookStoresinIndia, "Data Type Value 2":
BPBBookStoresinUSA});
db.BPBOnlineBooksDataTypesCollection.find({ "Data Type Alias":
"array"}).pretty();
Object Data Type
The binary data types are used to store the values in binary form
such as Base64. Sometimes, there are scenarios where we store
some objects like images (in a binary form) in database.
In our example, we have created a variable which contains a
binary data as follows:
Code 1
var BPBBooksBinaryData = BinData(1,
"SGVsbG8gV29ybGQgRnJvbSBCUEIgUHVibGljYXRpb25z");
And then, we have inserted this binary data type in our
MongoDB collection and the code for the same is shown in the
following screenshot:
Binary Data Type
Code 2
db.BPBOnlineBooksDataTypesCollection.insert({ "Data Type Alias":
"binData", "Data Type Number": "5", "Data Type Value":
BPBBooksBinaryData});
Object Id Data Types
Code 1
var BPBBooksObjectId = ObjectId();
And then, we have inserted this ObjectId data type in our
MongoDB
collection and the code for the same is shown in the following
screenshot:
Code 2
db.BPBOnlineBooksDataTypesCollection.insert({ "Data Type
Alias": "objectid", "Data Type Number": "7", "Data Type Value":
BPBBooksObjectId});
Date data Types
Date data type stores the date and time. These can be date or
date and time combined. There are various methods in
MongoDB to generate date and time, as shown in the following
table
Date Data Type Description
Date() This method returns the current
date in the
string format
Code 1
var BPBBooksDate1 = Date();
var BPBBooksDate2 = new Date();
And then, we have inserted these date data type variables in our
MongoDB
collection and the code for the same is shown in the following
screenshot:
Code 2
db.BPBOnlineBooksDataTypesCollection.insert({ "Data Type Alias":
"date", "Data Type Number": "9", "Data Type Value 1":
BPBBooksDate1, "Date Type Value 2": BPBBooksDate2});
Null Data Types
Null data type stores the null values. Null are the data types
which has no type or value, so it is called as null. Sometimes,
there are scenarios where we have to store something which
exists but is yet to be defined and we are not sure what type
and value it would have. In this case, we use null.
In our example, we have created a variable which contains a
null type value.
Code 1
var BPBBooksNull = null;
And then, we have inserted this null data type variable in our
MongoDB
collection and the code for the same is shown in the following
screenshot:
Null Data Types
Code 2
db.BPBOnlineBooksDataTypesCollection.insert({"Data Type
Alias": "null", "Data Type Number": "10","Data Type Value":
BPBBooksNull});
Code 3
db.BPBOnlineBooksDataTypesCollection.find({"Data Type Alias":
"null"}).pretty();
Regular Expression Data
Types
Code 2
db.BPBOnlineBooksDataTypesCollection.insert({"Data Type Alias":
"regex", "Data Type Number": "11", "Data Type Value":
BPBBooksRegEx});
Code 3
db.BPBOnlineBooksDataTypesCollection.find({"Data Type Alias":
"regex"}).pretty();
Java Script data types(without
scope)
Code 2
db.BPBOnlineBooksDataTypesCollection.insert({"Data Type Alias":
"javascript", "Data Type Number": "13", "Data Type Value Function":
BPBBooksFunction, "Data Type Value Function Scope":
BPBBooksFunctionScope});
Java Script data types(with
scope)
Code 2
db.BPBOnlineBooksDataTypesCollection.insert({"Data Type Alias":
"javascriptWithScope", "Data Type Number": "15","Data Type Value
Function": BPBBooksFunction, "Data Type Value Function Scope":
BPBBooksFunctionScope});
Timestamp Data Types
Code 2
db.BPBOnlineBooksDataTypesCollection.insert({"Data Type
Alias": "timestamp", "Data Type Number": "17", "Data Type
Value": BPBBooksTimestamp});
Boolean data types
Boolean data type stores the boolean data which is either true
or false.
In our example, we have created two variables which contains a
"true value“ and a "false value" respectively.
Code 1
var BPBBooksBoolean1 = true;
var BPBBooksBoolean2 = false
Boolean data types
db.BPBOnlineBooksDataTypesCollection.insert({"Data Type
Alias": "bool","Data Type Number": "8", "Data Type Value 1":
BPBBooksBoolean1,"Data Type Value 2": BPBBooksBoolean2});
Min and MAX Key
In
MongoDB, the min and max keys
compare a value against the lowest
and the highest BSON elements.
The min key compares the values
of the lowest BSON element,
whereas the max key compares the
values of the highest BSON element
Decimal 128
Array
BinData
ObjectId
Boolean
Date
Timestamp
Regular expression
MaxKey
Comparison and Sort Order
Code 1
var BPBBooksVar1 = 100000;
var BPBBooksVar2 = "BPB Publications";
var BPBBooksVar3 = 77.07;
var BPBBooksVar4 = true;
var BPBBooksVar5 = null;
var BPBBooksVar6 = MinKey;
var BPBBooksVar7 = MaxKey;
Comparison and Sort Order
Code 2
db.BPBOnlineBooksDataTypesCollectionV2.insert([{ "Data Type
Alias": "int", "Data Type Value": BPBBooksVar1
}, { "Data Type Alias": "string", "Data Type Value": BPBBooksVar2},
{ "Data Type Alias": "double", "Data Type Value": BPBBooksVar3},
{"Data Type Alias": "bool","Data Type Value": BPBBooksVar4},
{ "Data Type Alias": "null", "Data Type Value": BPBBooksVar5},
{ "Data Type Alias": "minKey", "Data Type Value": BPBBooksVar6},
{
"Data Type Alias": "maxKey","Data Type Value": BPBBooksVar7}]);