1. Database Document
1 | P a g e
MONGODB
Database R&D Documentation
Document Control
Document Location
Location
10.0.1.96Team SharesDB_Activities MongoDB
Template History
Author Date Version Changes
Tharun B 07/09/2015 1.0 Document Created
Revision History
Author Date Versi
on
Revision Description
Tharun B 07/09/2015 1.0 Introduction to MongoDB
Tharun B 07/09/2015 1.0 Installation
Tharun B 10/09/2015 1.1 CRUD Operations, Aggregations, Data Modelling &
Referencing Techniques and Indexing
Tharun B 16/09/2015 1.2 Regular Expressions, Sequences, Capped Collections
Tharun B 16/09/2015 1.3 Creating JavaScript Functions
Tharun B 18/09/2015 1.4 Map Reduce Paradigm
Tharun B 21/09/2015 1.5 Operators, Commands and Mongo Shell Methods.
2. Database Document
2 | P a g e
Approvals
Name Role Reviewed Version
Approved
Approval Date
Kishore Kumar I Lead – DBA No NA NA
3. Database Document
3 | P a g e
WHAT IS MONGODB?
MongoDB is a cross platform, document oriented database that provides, high performance,
high availability and easy scalability.
MongoDB works on concept of Collection and document.
If you have one of the following challenges, you should consider MongoDB:
You Expect a High Write Load
MongoDB by default prefers high insert rate over transaction safety. If you need to load tons of
data lines with a low business value for each one, MongoDB should fit.
You need High Availability in an Unreliable Environment (Cloud and Real Life)
Setting replica Set (set of servers that act as Master-Slaves) is easy and fast. Moreover, recovery
from a node (or a data center) failure is instant, safe and automatic.
You need to Grow Big (and Shard Your Data)
Databases scaling is hard (a single MySQL table performance will degrade when crossing the 5-
10GB per table). If you need to partition and shard your database, MongoDB has a built in easy
solution for that.
Your Data is Location Based
MongoDB has built in spacial functions, so finding relevant data from specific locations is fast
and accurate. Geographically redundant replicas can be deployed that logically represented as a
single database.
Your Data Set is Going to be Big (starting from 1GB) and Schema is Not Stable
Adding new columns to RDBMS can lock the entire database in some database, or create a
major load and performance degradation in other. Usually it happens when table size is larger
than. As MongoDB is schema-less, adding a new field, does not effect old rows (or documents)
and will be instant.
4. Database Document
4 | P a g e
You Don't have a DBA
If you don't have a DBA, and you don't want to normalize your data and do joins, you should
consider MongoDB. Not only in terms of DBA’s , but also if you have regularly changing schemas
where using rdbms is little difficult, one should consider MongoDB as MongoDb doesn’t have
fixed schema.
Based on the above considerations, the MongoDB is suitable for applications that deal with the
following things:
Account and user profiles: can store arrays of addresses with ease
Form data: MongoDB makes it easy to evolve the structure of form data over time
Blogs / user-generated content: can keep data with complex relationships together in one
object
Messaging: vary message meta-data easily per message or message type without needing to
maintain separate collections or schemas
System configuration: just a nice object graph of configuration values, which is very natural in
MongoDB
Log data of any kind: structured log data is the future
Graphs: just objects and pointers – a perfect fit
Location based data: MongoDB understands geo-spatial coordinates and natively supports geo-
spatial indexing
5. Database Document
5 | P a g e
COMPARISON OF MONGODB VS RDMS:
RDMS MONGODB
Database Database
Table Collection
Row Document
Column Field
Primary Key Primary key (Default key_ID column provided by MongoDB itself)
Table Join Embedded documents
ADVANTAGES OF MONGODB:
6. Database Document
6 | P a g e
The main advantages of MongoDB over any RDBMS are the following:
Schema less - MongoDB is document database in which one collection holds different
documents, number of fields, contract and size of the document can be differ from one
documents to another.
Structure of a single object is clear.
No complex joins.
Deep query ability - Its supports dynamic queries on documents using a document-based query
language and its powerful as SQL.
Easy to Scale-out(Horizontal Scalable).
How data is Modeled in MongoDB?
7. Database Document
7 | P a g e
Data is stored in the form of JSON documents. A simple Key-value pair JSON looks like :
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},
{
user:'user2',
message: 'My second comments',
dateCreated: new Date(2011,1,25,7,45),
like: 5
}
]
}
8. Database Document
8 | P a g e
DATA MODELLING IN MONGODB
Modelling relationships
Relationships in MongoDB represent how various documents are logically related to each other.
Relationships can be modeled via Embedded and Referenced approaches. Such relationships can
be either 1:1, 1: N, N: 1 or N: N.
9. Database Document
9 | P a g e
Let us consider the case of storing addresses for users. So, one user can have multiple addresses
making this a 1: N relationship.
Following is the sample document structure of user document:
{
"_id":ObjectId("52ffc33cd85242f436000001"),
"name": "Tom Hanks",
"contact": "987654321",
"dob": "01-01-1991"
}
Following is the sample document structure of address document:
{
"_id":ObjectId("52ffc4a5d85242602e000000"),
"building": "22 A, Indiana Apt",
"pincode": 123456,
"city": "Los Angeles",
"state": "California"
}
MODELING EMBEDDEDRELATIONSHIPS
In the embedded approach, we will embed the address document inside the user document.
{
"_id":ObjectId("52ffc33cd85242f436000001"),
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin",
"address": [
{
10. Database Document
10 | P a g e
"building": "22 A, Indiana Apt",
"pincode": 123456,
"city": "Los Angeles",
"state": "California"
},
{
"building": "170 A, Acropolis Apt",
"pincode": 456789,
"city": "Chicago",
"state": "Illinois"
}]
}
This approach maintains all the related data in a single document which makes it easy to retrieve and
maintain. The whole document can be retrieved in a single query like this:
>db.users.findOne({"name":"Tom Benzamin"},{"address":1})
Note that in the above query, db and users are the database and collection respectively.
Embedded data model or embedded relationship model
11. Database Document
11 | P a g e
Note : The drawback is that if the embedded document keeps on growing too much in size, it can
impact the read/write performance.
MODELING REFERENCED RELATIONSHIPS
Thisis the approach of designing normalized relationship.In thisapproach,both the user and address
documents will be maintained separately but the user document will contain a field that will reference
the address document's id field.
{
"_id":ObjectId("52ffc33cd85242f436000001"),
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin",
"address_ids": [
ObjectId("52ffc4a5d85242602e000000"),
ObjectId("52ffc4a5d85242602e000001")
]
12. Database Document
12 | P a g e
}
As shown above, the user document contains the array field address_ids which contains ObjectIds
of corresponding addresses.Using these ObjectIds, we can query the address documents and get
address details from there. With this approach, we will need two queries: first to fetch
the address_ids fields from user document and second to fetch these addresses
from address collection.
>var result = db.users.findOne({"name":"Tom Benzamin"},{"address_ids":1})
>var addresses = db.address.find({"_id":{"$in":result["address_ids"]}})
Normalized data model or referenced relationship model:
13. Database Document
13 | P a g e
WHO ARE USING MONGODB?
https://www.mongodb.com/who-uses-mongodb
Please refer the above link to know the whole story.
Install, setupandstart MongoDB on Windows
Requirements:
14. Database Document
14 | P a g e
On Windows MongoDB requires Windows Server 2008 R2, Windows Vista, or
later.
Sources
We have two ways to download mongodb from the official site those are
1. .zip file
2. .msi file
The .msi installer includes all other software dependencies and will automatically upgrade
any older version of MongoDB installed using an .msi file.
BEFORE DOWNLOADING MONGODB FILES FROM OFFICIAL SITE WE HAVE TO DETERMINE WHICH MONGODB
BUILD YOU NEED BECAUSE THERE ARE THREE BUILDS OF MONGODB FOR WINDOWS THOSE ARE LISTED BELOW.
MongoDB for Windows 64-bit: Runs only on Windows Server 2008 R2, Windows 7 64-bit, and
newer versions of Windows. This build takes advantage of recent enhancements to the
Windows Platform and cannot operate on older versions of Windows.
MongoDB for Windows 32-bit: Runs on any 32-bit version of Windows newer than Windows
Vista. 32-bit versions of MongoDB are only intended for older systems and for use in testing
and development systems. 32-bit versions of MongoDB only support databases smaller than
2GB.
MongoDB for Windows 64-bit Legacy: Runs on Windows Vista, Windows Server 2003, and
Windows Server 2008 and does not include recent performance enhancements.
To find the version of windows you are running, open the command prompt and enter wmic os
get architecture.
Download MongoDB for Windows:
15. Database Document
15 | P a g e
Download the latest production release of MongoDB from the MongoDB downloads page.
Ensure you download the correct version of MongoDB for your Windows system. The 64-bit
versions of MongoDB do not work with 32-bit Windows.
InstallationSteps:
1. After downloading .msi file open Downloads folder in your system and run .msi file
(mongodb-win32-x86_64-2008plus-ssl-3.0.4-signed).
By clicking on .msi file the first step is welcome window
16. Database Document
16 | P a g e
2. Click ‘Next’ to move forward for the installation in this step we have accept the license
agreement.
17. Database Document
17 | P a g e
3. Click ‘Next’ to move step 3, In this step you have to select one option out of two either
‘complete’ or ‘custom’. Description about two options preset in below image.
4. Now Mongodb is ready to install just click on install button to move forward.
18. Database Document
18 | P a g e
5. In this step we can seethe installationstatus once installation of mongodb was succeeded
it will move to next step.
20. Database Document
20 | P a g e
After installation of mongodb open program files folder in your systemthere you can find
MONGODB folder. If you go through that folder you will identify some list of sub folders
like Server, 3.0, bin. If you open bin folder you can identify some list .exe and .dll files will
be present. Description of those file will be listed below.
Component Set Binaries
Server mongod.exe
Router mongos.exe
Client mongo.exe
MonitoringTools mongostat.exe, mongotop.exe
ImportExportTools mongodump.exe, mongorestore.exe, mongoexport.exe,mongoimport.exe
MiscellaneousTools bsondump.exe, mongofiles.exe, mongooplog.exe, mongoperf.exe
RUN MONGODB:
21. Database Document
21 | P a g e
1. SET UP THE MONGODB ENVIRONMENT:
Mongodb stores data in db folder within data folder. But, since this data folder is not
created automatically, you have to create it manually. Remember that data directory should be
created in the root (i.e. C: or D: or so).
In this document we have mongodb folder in C: drive so we have created data with db folders
in C drive itself.
2. Start Mongodb:
To start mongodb type mongod in command prompt like below.
Open bin folder from mongodb folder then right click on bin and select ‘open command window
here’ then command prompt window will open like below.
22. Database Document
22 | P a g e
After that type mongod
Next clickEnter command then server will start .For authentication ensured ,type mongod --auth
or for help type mongod -- help. See help for more options.
After successfully starting the server it will shows the port number like below.
23. Database Document
23 | P a g e
After that, we have to start client i.emongo for this you have to open one more command prompt
window as like above and type mongo. The connection can be made to a remote server if IP and
port number is known and mongod server is running .
By default test database will be created at that time of installation itself. We can create our own
databases with using ‘USE’ command.
Example: use mongodb_sysbiz. This will create mongodb_sysbiz DB.
24. Database Document
24 | P a g e
Note: Mongodb is pure case sensitive.
For listing number of collections present in a database we will use ‘show collections’ command.
For displaying Databases at least one collection should be present in a database. For creating
collection use db.createcollection (“sysbiz”) it will create sysbiz collection.
CRUD OPERATIONS:
Already we’ve seen how to use “use” command to create a database.
“use trainee_sysbiz” will create a database with the name trainee_sysbiz with some default
amount of memory allocated.
Similarly use the “use” command to switch between two dbs.
>use mydb
switched to db mydb
If you want to check your databases list, then use the command “show dbs”.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not present in list. To display database you need to insert atleast
one document into it.
>db.movie.insert({"name":"SPIDERMAN"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
In mongodb default database is test. If you didn't create any database then collections will be stored
in test database.
25. Database Document
25 | P a g e
THEDROPDATABASE()METHOD
SYNTAX:
Basic syntax of dropDatabase() command is as follows:
db.dropDatabase()
Thiswill delete the selected database. If you have notselected any database,then itwill delete default
'test' database
EXAMPLE:
First, check the list available databases by using the command show dbs
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want to delete new database <mydb>, then dropDatabase() command would be as follows:
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
Now check list of databases
>show dbs
local 0.78125GB
test 0.23012GB
>
THECREATECOLLECTION()METHOD
26. Database Document
26 | P a g e
MongoDB db.createCollection(name, options) is used to create collection, with options on size and
indexing. With inserting documents into a collection a collection is created implicitly. But to have
options on indexes and size of the collection we proceed by this method.
SYNTAX:
Basic syntax of createCollection() command is as follows
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and used to
specify configuration of collection
Parameter Type Description
Name String Name of the collection to be created
Options Document (Optional) Specify options about memory size
and indexing
Options parameter is optional, so you need to specify only name of the collection. Following is the list
of options you can use:
Field Type Description
capped Boolean (Optional) If true, enables a capped collection. Capped collection
is a collection fixed size collecction that automatically overwrites
its oldest entries when it reaches its maximum size. If you
specify true, you need to specify size parameter also.
autoIndexID Boolean (Optional) If true, automatically create index on _id field.s Default
value is false.
27. Database Document
27 | P a g e
size number (Optional) Specifies a maximum size in bytes for a capped
collection. If If capped is true, then you need to specify this
field also.
max number (Optional) Specifies the maximum number of documents allowed
in the capped collection.
While inserting the document, MongoDB first checks size field of capped collection, then it checks
max field.
EXAMPLES:
Basic syntax of createCollection() method without options is as follows
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can check the created collection by using the command show collections
>show collections
mycollection
system.indexes
Following example shows the syntax of createCollection() method with few important options:
The significance of the options will be seen at a later stage.
>db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000
} )
{ "ok" : 1 }
>
In mongodb you don't need to create collection as said above. MongoDB creates collection
automatically, when you insert some document. The following collection is created on insert
command.
28. Database Document
28 | P a g e
>db.employ.insert({"name" : "tharun srinivasa"})
>show collections
employ
mycollection
system.indexes
>
THEDROP()METHOD
MongoDB's db.collection.drop() is used to drop a collection from the database.
SYNTAX:
Basic syntax of drop() command is as follows. This swill simply drop the collection.
db.COLLECTION_NAME.drop()
DATATYPES AND USAGE :
MongoDB doesn’t require datatypes while performing create collections as we are not defining any
fields and their datatypes while creating collections. While inserting documents into collections
MongoDB implicitly identifies the datatype and defines that datatype to the respective field. However
when we want to insert certain data with certain datatype we should explicitly define it while inserting.
MongoDB supports many datatypes whose list is given below:
String : This is most commonly used datatype to store the data
Integer : Thistype is used to store a numerical value. Integer can be 32 bitor 64 bit depending
upon your server.
Boolean : This type is used to store a boolean (true/ false) value.
Double : This type is used to store floating point values.
29. Database Document
29 | P a g e
Min/ Max keys : This type is used to compare a value against the lowest and highest BSON
elements.
Arrays : This type is used to store arrays or list or multiple values into one key.
Timestamp : ctimestamp. This can be handy for recording when a document has been
modified or added.
Object : This datatype is used for embedded documents.
Null : This type is used to store a Null value.
Symbol : This datatype is used identically to a string however, it's generally reserved for
languages that use a specific symbol type.
Date : This datatype is used to store the current date or time in UNIX time format. You can
specify your own date time by creating object of Date and passing day, month, year into it.
Object ID : This datatype is used to store the document’s ID.
Binary data : This datatype is used to store binay data.
Code : This datatype is used to store javascript code into document.
Regular expression : This datatype is used to store regular expression
The important concepts about datatypes to know before proceeding further are listed below. The
following examples are only an illustration of how explicitly datatypes are used to store data in the
collections. For more info check out https://docs.mongodb.org/manual/core/shell-types/
Date
The mongo shell provides various methods to return the date, either as a string or as a Date object:
Date() method which returns the current date as a string.
new Date() constructor which returns a Date object using the ISODate() wrapper.
ISODate() constructor which returns a Date object using the ISODate() wrapper.
30. Database Document
30 | P a g e
Note: Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds
since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions
years into the past and future.
Return Date as a String
To return the date as a string, use the Date() method, as in the following example:
var myDateString = Date();
To print the value of the variable, type the variable name in the shell, as in the following:
myDateString
The result is the value of myDateString:
Wed Dec 19 2012 01:03:25 GMT-0500 (EST)
To verify the type, use the typeof operator, as in the following:
typeof myDateString
The operation returns string.
NumberLong
By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides the
NumberLong() wrapper to handle 64-bit integers.
The NumberLong() wrapper accepts the long as a string:
NumberLong("2090845886852")
The following examples use the NumberLong() wrapper to write to the collection:
db.collection.insert( { _id: 10, calc: NumberLong("2090845886852") } )
db.collection.update( { _id: 10 }, { $set: { calc: NumberLong("2555555000000") } } )
31. Database Document
31 | P a g e
NumberInt
By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides the
NumberInt() constructor to explicitly specify 32-bit integers.
THEINSERT()METHOD
To insert data into MongoDB collection, you need to use MongoDB's insert() or save()method.
SYNTAX
Basic syntax of insert() command is as follows:
>db.COLLECTION_NAME.insert(document)
EXAMPLE
>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
Here mycol is our collection name. If the collection doesn't exist in the database, then MongoDB will
create this collection and then insert document into it.
In the inserted document if we don't specify the _id parameter, then MongoDB assigns an unique
ObjectId for this document.
_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided
as follows:
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
To insert multiple documents in single query, you can pass an array of documents in insert()
command.
32. Database Document
32 | P a g e
EXAMPLE
>db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: 'NoSQL database doesn't have tables',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
To insert the document you can use db.post.save(document) also. If you don't specify _id in the
document then save() method will work same as insert() method. If you specify _id then it will
replace whole data of document containing _id as specified in save() method.
MongoDB's update() and save() methods are used to update document into a collection. The
update() method update values in the existing document while the save() method replaces the
existing document with the document passed in save() method.
33. Database Document
33 | P a g e
MONGODBUPDATE()METHOD
The update() method updates values in the existing document.
SYNTAX:
Basic syntax of update() method is as follows
>db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
EXAMPLE
Consider the mycol collectioin has following data.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
Following example will set the new title 'New MongoDB Tutorial' of the documents whose title is
'MongoDB Overview'
>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
>
Note: By default mongodb will update only single document, to update multiple you need to set a
paramter 'multi' to true.
>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB
Tutorial'}},{multi:true})
MONGODBSAVE()METHOD
The save() method replaces the existing document with the new document passed in save() method
SYNTAX
Basic syntax of mongodb save() method is shown below:
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
34. Database Document
34 | P a g e
EXAMPLE
Following example will replace the document with the _id '5983548781331adf45ec6'
>db.mycol.save({"_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Tutorial" })
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":" NoSQL Tutorial "}
>
THEREMOVE()METHOD
MongoDB's remove() method is used to remove document from the collection. remove() method
accepts two parameters. One is deletion criteria and second is justOne flag
deletion criteria : (Optional) deletion criteria according to documents will be removed.
justOne : (Optional) if set to true or 1, then remove only one document.
SYNTAX:
Basic syntax of remove() method is as follows
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
EXAMPLE
Consider the mycol collectioin has following data.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
Following example will remove all the documents whose title is 'MongoDB Overview'
>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
>
35. Database Document
35 | P a g e
REMOVEONLYONE
If there are multiple records and you want to delete only first record, then set justOne parameter
in remove() method
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
REMOVEALLDOCUMENTS
If you don't specify deletion criteria, then mongodb will delete whole documents from the
collection. This is equivalent of SQL's truncate command.
>db.mycol.remove()
THEFIND()METHOD
To query data from MongoDB collection, you need to use MongoDB's find() method.
SYNTAX
Basic syntax of find() method is as follows
>db.COLLECTION_NAME.find()
find() method will display all the documents in a non structured way.
In the find method there are two {},{} like this
db.mycol.find( {condition} , {the fields in a document you want to display };
Incase if you want all the fields in a document to display that is (SELECT * FROM) , then it would be
something like the following:
db.mycol.find( {condition});
THEPRETTY()METHOD
To display the results in a formatted way, you can use pretty() method.
36. Database Document
36 | P a g e
SYNTAX:
>db.mycol.find().pretty()
EXAMPLE
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview", "description": "MongoDB is no sql database",
"tags": ["mongodb", "database", "NoSQL"],"likes": "100
}
Apart from find() method there is findOne() method, that reruns only one document.
RDBMSWHERECLAUSEEQUIVALENTSINMONGODB
To query the document on the basis of some condition, you can use following operations
Operation Syntax Example RDBMS
Equivalent
Equality {<key>:<value>} db.mycol.find({"by":"tharun"}).pretty() where by =
‘tharun'
Less Than {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes
< 50
Less Than
Equals
{<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes
<= 50
37. Database Document
37 | P a g e
Greater
Than
{<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes
> 50
Greater
Than
Equals
{<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes
>= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes
!= 50
ANDINMONGODB
SYNTAX:
In the find() method if you pass multiple keys by separating them by ',' then MongoDB treats
it AND condition. Basic syntax of AND is shown below:
>db.mycol.find({key1:value1, key2:value2}).pretty()
EXAMPLE
Below given example will show all the tutorials written by 'tutorials point' and whose title is 'MongoDB
Overview'
>db.mycol.find({"by":"tharun","title": "MongoDB Overview"}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tharun",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
38. Database Document
38 | P a g e
For the above given example equivalent where clause will be ' where by='tutorials point' AND
title='MongoDB Overview' '. You can pass any number of key, value pairs in find clause.
OR INMONGODB
SYNTAX:
To querydocuments based on the OR condition,you need to use $or keyword. Basic syntaxof OR is
shown below:
>db.mycol.find({$or: [{key1: value1}, {key2:value2}]}).pretty()
EXAMPLE
Below given example will show all the tutorials written by 'tutorials point' or whose title is 'MongoDB
Overview'
>db.mycol.find({$or:[{"by":"tharun"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tharun",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
USINGANDANDOR TOGETHER
EXAMPLE
Below given example will show the documents that have likes greater than 100 and whose title is
either 'MongoDB Overview' or by is 'tutorials point'. Equivalent sql where clause is'where likes>10
AND (by = 'tutorials point' OR title = 'MongoDB Overview')'
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tharun"},{"title": "MongoDB
Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
39. Database Document
39 | P a g e
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tharun", "tags": ["mongodb", "database", "NoSQL"],"likes": "100"}
>
We often come across situations where we need to limit the number of documents returned by
a query. Few a times we also fall in a situation where we need to select few documents by
skipping a few count. For such requirements , we have :
THELIMIT()METHOD
To limit the records in MongoDB, you need to use limit() method. limit() method accepts one number
type argument, which is number of documents that you want to displayed.
SYNTAX:
Basic syntax of limit() method is as follows
>db.COLLECTION_NAME.find().limit(NUMBER)
EXAMPLE
Consider the collection myycol has the following data
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tharun DB"}
Following example will display only 2 documents while quering the document.
>db.mycol.find({},{"title":1,_id:0}).limit(2)
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
>
If you don't specify number argument in limit() method then it will display all documents from the
collection.
40. Database Document
40 | P a g e
MONGODBSKIP()METHOD
Apart from limit() method there is one more method skip() which also accepts number type argument
and used to skip number of documents.
SYNTAX:
Basic syntax of skip() method is as follows
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
EXAMPLE:
Following example will only display only second document.
>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)
{"title":"NoSQL Overview"}
>
NOTE: Please note default value in skip() method is 0
To sort the documents in any order we have the following method:
THESORT()METHOD
To sort documents in MongoDB, you need to use sort() method. sort() method accepts a document
containing list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is
used for ascending order while -1 is used for descending order.
SYNTAX:
Basic syntax of sort() method is as follows
>db.COLLECTION_NAME.find().sort({KEY:1})
41. Database Document
41 | P a g e
EXAMPLE
Consider the collection myycol has the following data
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tharun Srinivasa"}
Following example will display the documents sorted by title in descending order.
>db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
{"title":"Tharun Srinivasa"}
{"title":"NoSQL Overview"}
{"title":"MongoDB Overview"}
>
sort() method will display documents in ascending order by default.
INDEXES IN MONGODB
Without indexes, MongoDB must scan every document of a collection to select those documents that
match the query statement. This scan is highly inefficientand require the mongodb to process a large
volume of data.Indexes are special data structures, that store a small portion of the data set in an
easy to traverse form. The index stores the value of a specific field or set of fields, ordered by the
value of the field as specified in index.
THEENSUREINDEX() METHOD
To create an index you need to use ensureIndex() method of mongodb.
SYNTAX:
Basic syntax of ensureIndex() method is as follows()
>db.COLLECTION_NAME.ensureIndex({KEY:1})
42. Database Document
42 | P a g e
Here key is the name of filed on which you want to create index and 1 is for ascending order. To
create index in descending order you need to use -1.
EXAMPLE
>db.mycol.ensureIndex({"title":1})
>
In ensureIndex() method you can pass multiple fields, to create index on multiple fields.
>db.mycol.ensureIndex({"title":1,"description":-1})
>
ensureIndex() method also accepts list of options (which are optional), whose list is given below:
Parameter Type Description
background Boolean Builds the index in the background so that building an
index does not block other database activities. Specify
true to build in the background. The default value is
false.
unique Boolean Creates a unique index so that the collection will not
accept insertion of documents where the index key or
keys match an existing value in the index. Specify true
to create a unique index. The default value is false.
name string The name of the index. If unspecified, MongoDB
generates an index name by concatenating the names
of the indexed fields and the sort order.
dropDups Boolean Creates a unique index on a field that may have
duplicates. MongoDB indexes only the first occurrence
of a key and removes all documents from the collection
43. Database Document
43 | P a g e
thatcontain subsequentoccurrencesofthatkey. Specify
true to create unique index. The default value is false.
sparse Boolean If true, the index only references documents with the
specified field. These indexes use less space but
behave differently in some situations (particularly sorts).
The default value is false.
expireAfterSeconds integer Specifies a value, in seconds, as a TTL to control how
long MongoDB retains documents in this collection.
v index version The index version number. The default index version
depends on the version of mongodb running when
creating the index.
weights document The weight is a number ranging from 1 to 99,999 and
denotes the significance of the field relative to the other
indexed fields in terms of the score.
default_language string For a text index, the language that determines the list of
stop words and the rules for the stemmer and tokenizer.
The default value is English.
language_override string For a text index, specify the name of the field in the
document that contains, the language to override the
default language. The default value is language.
WHAT ISACOVEREDQUERY
A covered query is a query in which:
all the fields in the query are a part of an index and
all the fields returned in the query are in the same index
44. Database Document
44 | P a g e
Since all the fields present in the query are part of an index, MongoDB matches the query conditions
and returns the result using the same indexwithout actually looking inside documents. Since indexes
are present in RAM, fetching data from indexes is much faster as compared to fetching data by
scanning documents. For better understanding check out the following example.
USINGCOVEREDQUERIES
To test covered queries, consider the following document in users collection:
{"_id": ObjectId("53402597d852426020000002"),
"dob": "01-01-1991",
"gender": "M",
"name": "Tom Benzamin",
"user_name": "tombenzamin"
}
We will first create a compound index for users collection on fields gender and user_name using
following query:
>db.users.ensureIndex({gender:1,user_name:1})
Now, this index will cover the following query:
>db.users.find({gender:"M"},{user_name:1,_id:0})
That is to say that for the above query, MongoDB would not go looking into database documents.
Instead it would fetch the required data from indexed data which is very fast.
Since our index does not include _id field, we have explicitly excluded it from result set of our query
as MongoDB by default returns _id field in every query. So the following query would not have been
covered inside the index created above:
>db.users.find({gender:"M"},{user_name:1})
Lastly, remember that an index cannot cover a query if:
any of the indexed fields is an array
any of the indexed fields is a subdocument
45. Database Document
45 | P a g e
Aggregation
Aggregations are operations that process data records and return computed results. MongoDB
provides a rich set of aggregation operations that examine and perform calculations on the data
sets. Like queries, aggregation operations in MongoDB use collections of documents as an input
and return results in the form of one or more documents. The basic overview of aggregation is
given below, followed by detailed examples.
THEAGGREGATE() METHOD
For the aggregation in mongodb you should use aggregate() method.
SYNTAX:
Basic syntax of aggregate() method is as follows
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
EXAMPLES:
{
_id: ObjectId(7df78ad8902c), title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by_user: 'Tharun1067,url: ‘SYSBIZ.COM’,
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
_id: ObjectId(7df78ad8902d),title: 'NoSQL Overview',
description: 'No sql database is very fast',
by_user: 'Tharun1067',url: SYSBIZ.COM',
tags: ['mongodb', 'database', 'NoSQL'],likes: 10
},
46. Database Document
46 | P a g e
{
_id: ObjectId(7df78ad8902e),
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: 'Neo4j',
url: 'http://www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'],
likes: 750
},
Now from the above collection if you want to display a list that how many tutorials are written by each
user then you will use aggregate() method as shown below:
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
This will fetch the following result:
{"result" :
[
{
"_id" : "Tharun1067",
"num_tutorial" : 2
},
{
"_id" : "Neo4j",
"num_tutorial" : 1
}
],"ok" : 1
}
>
Sql equivalent query for the above use case will be:
Select by_user, count(*) from mycol group by by_use
Below is a list of available aggregation expressions:
47. Database Document
47 | P a g e
Expression Description Example
$sum Sums up the defined value from all documents in the collection. db.mycol.aggregate([{$group :
{_id : "$by_user", num_tutorial :
{$sum : "$likes"}}}])
$avg Calculates the average of all given values from all documents in the collection. db.mycol.aggregate([{$group :
{_id : "$by_user", num_tutorial :
{$avg : "$likes"}}}])
$min Gets the minimum of the corresponding values from all documents in the
collection.
db.mycol.aggregate([{$group :
{_id : "$by_user", num_tutorial :
{$min : "$likes"}}}])
$max Gets the maximum of the corresponding values from all documents in the
collection.
db.mycol.aggregate([{$group :
{_id : "$by_user", num_tutorial :
{$max : "$likes"}}}])
$push Inserts the value to an array in the resulting document. db.mycol.aggregate([{$group :
{_id : "$by_user", url : {$push:
"$url"}}}])
$addToSet Inserts the value to an array in the resulting document but does not create
duplicates.
db.mycol.aggregate([{$group :
{_id : "$by_user", url :
{$addToSet : "$url"}}}])
$first Gets the first document from the source documents according to the grouping.
Typically this makes only sense together with some previously applied “$sort”-
stage.
db.mycol.aggregate([{$group :
{_id : "$by_user", first_url :
{$first : "$url"}}}])
$last Gets the last document from the source documents according to the grouping.
Typically this makes only sense together with some previously applied “$sort”-
stage.
db.mycol.aggregate([{$group :
{_id : "$by_user", last_url :
{$last : "$url"}}}])
48. Database Document
48 | P a g e
PIPELINECONCEPT
In UNIX command shell pipeline means the possibility to execute an operation on some input and
use the output as the input for the next command and so on. MongoDB also support same concept
in aggregation framework. There is a set of possible stages and each of those is taken a set of
documents as an input and is producing a resulting set of documents (or the final resulting JSON
document at the end of the pipeline). This can then in turn again be used for the next stage an so on.
Possible stages in aggregation framework are following:
$project: Used to select some specific fields from a collection.
$match: This is a filtering operation and thus this can reduce the amount of documents that
are given as input to the next stage.
$group: This does the actual aggregation as discussed above.
$sort: Sorts the documents.
$skip: With this it is possible to skip forward in the list of documents for a given amount of
documents.
$limit: This limits the amount of documents to look at by the given number starting from the
current positions
$unwind: This is used to unwind document that are using arrays. when using an array the
data is kind of pre-joinded and this operation will be undone with this to have individual
documents again. Thus with this stage we will increase the amount of documents for the next
stage.
For more examples using all the aggregation expressions and pipeline parameters please refer
the following links:
http://docs.mongodb.org/manual/tutorial/aggregation-zip-code-data-set/
http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/
http://docs.mongodb.org/manual/tutorial/map-reduce-examples/
49. Database Document
49 | P a g e
MONGODB-DATABASE REFERENCES
To implement a normalized database structure in MongoDB we use the concept of Referenced
Relationships also referred to as Manual References in which we manually store the referenced
document'sid inside other document. However, in cases where a document contains referencesfrom
different collections, we can use MongoDB DBRefs.
DBREFS VS MANUALREFERENCES
As an example scenario where we would use DBRefs instead of Manual References, consider a
database where we are storing different types of addresses (home, office, mailing, etc) in different
collections (address_home, address_office, address_mailing, etc). Now, when a user collection's
document references an address, it also needs to specify which collection to look into based on the
address type. In such scenarios where a document references documents from many collections, we
should use DBRefs.
USINGDBREFS
There are three fields in DBRefs:
$ref: This field specifies the collection of the referenced document
$id: This field specifies the _id field of the referenced document
$db: This is an optional field and contains name of the database in which the referenced document
lies
Consider a sample user document having DBRef field address as shown below:
{
"_id":ObjectId("53402597d852426020000002"),
"address": {
"$ref": "address_home",
"$id": ObjectId("534009e4d852427820000002"),
"$db": "test"},
50. Database Document
50 | P a g e
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin"
}
The address DBRef field here specifies that the referenced address document lies
inaddress_home collection under test database and has an id of 534009e4d852427820000002.
The following code dynamically looks in the collection specified by $ref parameter
(address_home in our case) for a document with id as specified by $id parameter in DBRef.
>var user = db.users.findOne({"name":"Tom Benzamin"})
>var dbRef = user.address
>db[dbRef.$ref].findOne({"_id":(dbRef.$id)})
The above code returns the following address document present in address_homecollection:
{
"_id" : ObjectId("534009e4d852427820000002"),
"building" : "22 A, Indiana Apt",
"pincode" : 123456,
"city" : "Los Angeles",
"state" : "California"
}
Advanced Indexing:
Consider the following document of users collection:
{
51. Database Document
51 | P a g e
"address": {
"city": "Los Angeles",
"state": "California",
"pincode": "123"
},
"tags": [
"music",
"cricket",
"blogs"
],
"name": "Tom Benzamin"
}
Here if we want to search any user documents based on field tags, we will create an index on tags
array in the collection. Creating an index on array in turn creates separate indexentries for each of its
fields. So in our case when we create an index on tags array, separate indexes will be created for its
values music, cricket and blogs.
To create an index on tags array, use the following code:
>db.users.ensureIndex({"tags":1})
After creating the index, we can search on the tags field of the collection like this:
>db.users.find({tags:"cricket"})
To verify that proper indexing is used, use the following explain command:
>db.users.find({tags:"cricket"}).explain()
INDEXING SUB-DOCUMENTFIELDS:
Suppose that we want to search documents based on city, state and pincode fields. Since all these
fields are part of address sub-document field, we will create index on all the fields of the sub-
document.
52. Database Document
52 | P a g e
For creating index on all the three fields of the sub-document, use the following code:
>db.users.ensureIndex({"address.city":1,"address.state":1,"address.pincode":1})
Once the index is created, we can search for any of the sub-document fields utilizing this index as
follows:
>db.users.find({"address.city":"Los Angeles"})
Remember that the query expression has to follow the order of the index specified. So the index
created above would support the following queries:
>db.users.find({"address.city":"Los Angeles","address.state":"California"})
ENABLINGTEXTSEARCH BYCREATINGTEXT INDEX :
Sometimes its really important to search a document whose content or data is known but
completely unaware of collection, and field name. In such cases searching db for such data is
merely done by querying all the tables. By creating text index on the fields of a collection its easy
to query using the indexes.The Text Search uses stemming techniques to look for specifiedwords
in the string fields by dropping stemming stop words like a, an, the, etc. At present, MongoDB
supports around 15 languages.
CREATINGTEXTINDEX:
Consider the following document under posts collection containing the post text and its tags:
{
"post_text": "enjoy the mongodb articles by tharun",
"tags": ["mongodb","Tharun”]
}
We will create a text index on post_text field so that we can search inside our posts' text:
>db.posts.ensureIndex({post_text:"text"})
USINGTEXTINDEX:
53. Database Document
53 | P a g e
Now that we have created the text index on post_text field, we will search for all the posts that have
word tutorialspoint in their text.
>db.posts.find({$text:{$search:"tharun"}})
The above command returned the following result documents having ‘tharun’ word in their post text:
{
"_id" : ObjectId("53493d14d852429c10000002"),
"post_text" : "enjoy the mongodb articles by tharun",
"tags" : [ "mongodb", "tharun" ]
}
{
"_id" : ObjectId("53493d1fd852429c10000003"),
"post_text" : "writing tutorials on mongodb",
"tags" : [ "mongodb", "tutorial" ]
}
If you are using old versions of MongoDB, you have to use the following command:
>db.posts.runCommand("text",{search:" tharun "})
Using Text Search highly improves the search efficiency as compared to normal search.
DELETINGTEXTINDEX:
To delete an existing text index, first find the name of index using following query:
>db.posts.getIndexes()
After getting the name of your index from above query, run the following command.
Here,post_text_text is the name of the index.
>db.posts.dropIndex("post_text_text")
Indexing Limitations
EXTRAOVERHEAD:
54. Database Document
54 | P a g e
Every index occupies some space as well as causes an overhead on each insert, update and delete.
So if you rarely use your collection for read operations, it makes sense not to use indexes.
RAMUSAGE:
Since indexes are stored in RAM, you should make sure that the total size of the index does no t
exceed the RAM limit. If the total size increases the RAM size, it will start deleting some indexes and
hence causing performance loss.
QUERY LIMITATIONS:
Indexing can't be used in queries which use:
Regular expressions or negation operators like $nin, $not, etc.
Arithmetic operators like $mod, etc.
$where clause
Hence, it is always advisable to check the index usage for your queries.
INDEX KEYLIMITS:
Starting from version 2.6, MongoDB will not create an indexif the value of existing index field exceeds
the index key limit.
INSERTINGDOCUMENTSEXCEEDING INDEX KEYLIMIT:
MongoDB will not insert any document into an indexed collection if the indexed field value of this
documentexceeds the index keylimit. Same is the case with mongorestore and mongoimportutilities.
MAXIMUMRANGES:
A collection cannot have more than 64 indexes.
The length of the index name cannot be longer than 125 characters
A compound index can have maximum 31 fields indexed
Transactions In MongoDB:
55. Database Document
55 | P a g e
MongoDB does not support multi-document atomic transactions. However, it does provide atomic
operations on a single document. So if a document has hundred fields the update statement will
either update all the fields or none, hence maintaining atomicity at document-level.
MODEL DATAFORATOMICOPERATIONS
The recommended approach to maintain atomicity would be to keep all the related information which
is frequently updated together in a single document using embedded documents. This would make
sure that all the updates for a single document are atomic.
Consider the following products document:
{
"_id":1,
"product_name": "Samsung S3",
"category": "mobiles",
"product_total": 5,
"product_available": 3,
"product_bought_by": [
{
"customer": "john",
"date": "7-Jan-2014"
},
{
"customer": "mark",
"date": "8-Jan-2014"
}
]
}
In this document, we have embedded the information of customer who buys the product in
the product_bought_by field. Now, whenever a new customer buys the product, we will first check
56. Database Document
56 | P a g e
if the product is still available using product_available field. If available, we will reduce the value of
product_available field as well as insert the new customer's embedded document in the
product_bought_by field. We will use findAndModify command for this functionality because it
searches and updates the document in the same go.
>db.products.findAndModify({
query:{_id:2,product_available:{$gt:0}},
update:{
$inc:{product_available:-1},
$push:{product_bought_by:{customer:"rob",date:"9-Jan-2014"}}
}
})
Our approach of embedded document and using findAndModify query makes sure that the product
purchase information is updated only if it the product is available. And the whole of this transaction
being in the same query, is atomic.
For multi-document atomic transactions MongoDB performs a two phase commit to achieve
atomicity.
For more information on two phase commit check out the following link
http://docs.mongodb.org/master/tutorial/perform-two-phase-commits/
The above link contains an example where transactions at multi-document level are performed by two
phase commit technique.
regexExpression :
Regular Expressions are frequentlyused in all languagesto search for a pattern or word in any string.
MongoDB also provides functionality of regular expression for string pattern matching using
the $regex operator
Unlike text search, we do not need to do any configuration or command to use regular expressions.
57. Database Document
57 | P a g e
Consider the following document structure under posts collection containing the post text and its
tags:
{
"post_text": "enjoy the mongodb articles by tharun",
"tags": [
"mongodb",
"tharun"
]
}
USINGREGEXEXPRESSION
The following regex query searches for all the posts containing string tutorialspoint in it:
>db.posts.find({post_text:{$regex:"tharun"}})
The same query can also be written as:
>db.posts.find({post_text:/tharun/})
USINGREGEXEXPRESSIONWITHCASEINSENSITIVE
To make the search case insensitive, we use the $options parameter with value $i.The following
command will look for strings having word tutorialspoint,irrespective of smaller or capital case:
>db.posts.find({post_text:{$regex:"tharun",$options:"$i"}})
One of the results retuned from this query is following document which contains word tutorialspoint
in different cases:
{
"_id" : ObjectId("53493d37d852429c10000004"),
"post_text" : "hey! this is my post by tharun",
"tags" : [ "tharun" ]
}
USINGREGEXFORARRAY ELEMENTS:
58. Database Document
58 | P a g e
We can also use the concept of regex on array field. This is particularly very important when we
implement the functionality of tags. So, if you want to search for all the posts having tags beginning
from word tutorial (either tutorial or tutorials or tutorialpoint or tutorialphp), you can use the following
code:
>db.posts.find({tags:{$regex:"tharun"}})
OPTIMIZINGREGULAREXPRESSIONQUERIES:
If the document fields are indexed, the query will use make use of indexed values to match
the regular expression. This makes the search very fast as compared to the regular
expression scanning the whole collection.
If the regular expression is a prefix expression, all the matches are meant to start with a
certain string characters. For e.g., if the regexexpression is ^tha, then the query has to
search for only those strings that begin with tha.
Capped collections are fixed-size circular collections that follow the insertion order to support
high performance for create, read and delete operations. By the word Circular means oldest records
get deleted once new records comes in.
Capped collections restrict updates to the documents if the update results in increased document
size. Since capped collections store documents in the order of the disk storage, it ensures that the
document size does not increase the size allocated on disk.
NOTE : The scenario where Capped collections are best for storing are storing log information.
CREATINGCAPPEDCOLLECTION:
To create a capped collection, we use the normal createCollection command but withcapped option
as true and specifying the maximum size of collection in bytes.
>db.createCollection("cappedLogCollection",{capped:true,size:10000})
In addition to collection size, we can also limit the number of documents in the collection using
the max parameter:
59. Database Document
59 | P a g e
>db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})
If you want to check whether a collection is capped or not, use the following isCappedcommand:
>db.cappedLogCollection.isCapped()
If there is an existing collection which you are planning to convert to capped, you can do it with the
following code:
>db.runCommand({"convertToCapped":"posts",size:10000})
This code would convert our existing collection posts to a capped collection.
QUERYING CAPPEDCOLLECTION:
By default a find query on a capped collection will display results in insertion order. But if you want
the documents to be retrieved in reverse order, use the sort command as shown in the following
code:
>db.cappedLogCollection.find().sort({$natural:-1})
There are few other important points regarding capped collections worth knowing:
We cannot delete documents from a capped collection
There are no default indexes present in a capped collection, not even on _id field
While inserting a new document, MongoDB does not have to actually look for a place to
accommodate new document on the disk. It can blindly insert the new document at the tail of
the collection. This makes insert operations in capped collections very fast.
Similarly, while reading documents MongoDB has just to return documents in the same order
as present on disk. This makes the read operation very fast.
AUTO INCREMENT SEQUENCE
60. Database Document
60 | P a g e
By default MongoDB uses the 12-byte ObjectId for the _id field as primary key to uniquely identify
the documents. However, there may be scenarios where we may want the _id field to have some
auto-incremented value other than the ObjectId.
Since this is not a default feature in MongoDB, we will programmatically achieve this functionality by
using a counters collection as suggested by the MongoDB documentation.
USINGCOUNTERCOLLECTION
Consider the following products document. We want the _id field to be an auto-incremented
integer sequence starting from 1,2,3,4 upto n.
{
"_id":1,
"product_name": "Apple iPhone",
"category": "mobiles"
}
For this, create a counters collection which will keep track of the last sequence value for all the
sequence fields.
>db.createCollection("counters")
Now, we will insert the following document in the counters collection with productid as its key:
{
"_id":"productid",
"sequence_value": 0
}
The field sequence_value keeps track of the last value of the sequence.
61. Database Document
61 | P a g e
CREATINGJAVASCRIPTFUNCTION
Now, we will create a function getNextSequenceValue which will take the sequence name as its
input, increment the sequence number by 1 and return the updated sequence number. In our case,
the sequence name is productid.
>function getNextSequenceValue(sequenceName){
var sequenceDocument = db.counters.findAndModify(
{
query:{_id: sequenceName },
update: {$inc:{sequence_value:1}},
new:true
});
return sequenceDocument.sequence_value;
}
USINGTHEJAVASCRIPTFUNCTION:
We will now use the function getNextSequenceValue while creating a new document and assigning
the returned sequence value as document's _id field.
Insert two sample documents using the following code:
>db.products.insert({
"_id":getNextSequenceValue("productid"),
"product_name":"Apple iPhone",
"category":"mobiles"})
>db.products.insert({
"_id":getNextSequenceValue("productid"),
"product_name":"Samsung S3",
"category":"mobiles"})
As you can see, we have used the getNextSequenceValue function to set value for the _id field.To
verify the functionality, let us fetch the documents using find command:
62. Database Document
62 | P a g e
>db.prodcuts.find()
The above query returned the following documents having the auto-incremented _id field:
{ "_id" : 1, "product_name" : "Apple iPhone", "category" : "mobiles"}
{ "_id" : 2, "product_name" : "Samsung S3", "category" : "mobiles" }
MAPREDUCECOMMAND:
63. Database Document
63 | P a g e
Map-reduce is a data processing paradigm for condensing large volumes of data into useful
aggregated results. MongoDB uses mapReduce command for map-reduce operations. MapReduce
is generally used for processing large data sets.
Following is the syntax of the basic mapReduce command:
>db.collection.mapReduce(
function() {emit(key,value);}, //map function
function(key,values) {return reduceFunction}, //reduce function
{
out: collection,
query: document,
sort: document,
limit: number
}
)
The map-reduce function first queries the collection, then maps the result documents to emit key-
value pairs which is then reduced based on the keys that have multiple values.
In the above syntax:
map is a javascript function that maps a value with a key and emits a key-valur pair
reduce is a javscript function that reduces or groups all the documents having the same key
out specifies the location of the map-reduce query result
query specifies the optional selection criteria for selecting documents
sort specifies the optional sort criteria
limit specifies the optional maximum number of documents to be returned
USINGMAPREDUCE:
Consider the following document structure storing user posts. The document stores user_name of
the user and the status of post.
{
"post_text": "tutorialspoint is an awesome website for tutorials",
64. Database Document
64 | P a g e
"user_name": "mark",
"status":"active"
}
Now, we will use a mapReduce function on our posts collection to select all the active posts, group
them on the basis of user_name and then count the number of posts by each user using the following
code:
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)},
{
query:{status:"active"},
out:"post_total"
}
)
The above mapReduce query outputs the following result:
{
"result" : "post_total",
"timeMillis" : 9,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 2
},
"ok" : 1,
}
The result shows that a total of 4 documents matched the query (status:"active"), the map function
emitted 4 documentswith key-value pairs and finally the reduce function grouped mapped documents
having the same keys into 2.
65. Database Document
65 | P a g e
To see the result of this mapReduce query use the find operator:
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)},
{
query:{status:"active"},
out:"post_total"
}
).find()
The above query gives the following result which indicates that both users tom and markhave two
posts in active states:
{ "_id" : "tom", "value" : 2 }
{ "_id" : "mark", "value" : 2 }
In similar manner, MapReduce queries can be used to construct large complex aggregation queries.
The use of custom Javascript functions makes usage of MapReduce very flexible and powerful.
Operators , Database Commands and Mongo Shell Methods
For the reference of all the operators, database commands and Mongo Shell Methods used in MongoDB please
refer the following links. The following links have complete info and with few examples.
http://docs.mongodb.org/manual/reference/operator/
http://docs.mongodb.org/manual/reference/command/
http://docs.mongodb.org/manual/reference/method/