0% found this document useful (0 votes)
23 views80 pages

DSS - U3 - Chap6 - MongoDB Rev 1.1

MongoDB(BDA)

Uploaded by

mbhavya5867
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views80 pages

DSS - U3 - Chap6 - MongoDB Rev 1.1

MongoDB(BDA)

Uploaded by

mbhavya5867
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #1

Introduction to MongoDB

New kind of database is gaining ground in the enterprise called NoSQL (Not only SQL). We explore a NoSQL
database called "MongoDB".

WHAT IS MONGODB?
MongoDB is
1. Cross-platform.
2. Open source.
3. Non-relational.
4. Distributed.
5. NoSQL.
6. Document-oriented data store.

WHY MONGODB?
- Deal with large volumes of data,
- Rich variety of data-particularly unstructured data
- Scaling of enterprise data - database that can scale out or scale horizontally to meet the scale requirements
- Flexibile schema
- Fault tolerant,
- Consistent and Partition tolerant (CAP theorem or Brewers theorem)
- Easily distributed over a multitude of nodes in a cluster.

Capped collections are fixed-size collections that support high-throughput operations that insert and
retrieve documents based on insertion order

Using Java Script Object Notation (JSON)

JSON is extremely expressive. MongoDB actually does not use JSON but BSON (pronounced Bee Son) - it is
Binary JSON. It is an open standard, it is used to store complex data structures.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #2

Let us trace the journey from .CSV to XML to JSON:

Let us look CSV file having data is about the employees of “XYZ” organization.. Here we can see, that
column values are separated using commas and the rows are separated by a carriage return.

Syam, Sunder, +91 9848145670


Anu, Singeri, +91 7890012345
Pranay, Ramineni, +91 9123456789

This looks good! However let us make it slightly more legible by adding column heading.

FirstName, SurName, ContactNo


Syam, Sunder, +91 9848145670
Anu, Singeri, +91 7890012345
Pranay, Ramineni, +91 9123456789

Suppose if few employees have more than one ContactNo. It can be neatly classified as OfficeContactNo and
HomeContactNo. Or
if few employees have more than one Office Contact No and more than one Home Contact No?

Ok, so this is the first issue we need to address.

Another example
In case of employees where we need to store their email addresses as well.
Similarily we have the same issues, few employees have two email addresses, few have three and there are a
few employees with more than three email addresses as well.

Here CSV are known to store data well if it is flat and does not have repeating values.

This problem can be solved by XML. But as the name suggests XML is highly extensible. It does not just
call for defining a data format, rather it defines how you define a data format. You may be prepared to
undertake this cumbersome task for highly complex and structured data; however, for simple data exchange it
might just be too much work.

Enter JSON! Let us look at how it reacts to the problem at hand.


{
FirstName: Syam,
SurName: Sunder,
ContactNo: [+91 9848145670, +91 9848123670 ]
}
{
FirstName: Anu,
SurName: Singeri,
ContactNo: [+91 7890012345, +91 94456 46666]
}
{
FirstName: Pranay,
SurName: Ramineni,
ContactNo: +91 9123456789
}

As you can see it is quite easy to read a JSON. There is absolutely no confusion now. One can have a list of
contact numbers, and they can be stored with ease.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #3

JSON is very expressive. JSON provide easy to store and retrieve documents in their real form.

The binary form of JSON is BSON.

BSON is an open standard, it consumes less space as compared to the text-based JSON.

Major advantage with BSON, It is easier and quicker to convert BSON to a programming language's
native data format.

MongoDB drivers available for a number of programming languages such as C, C++, Ruby, PHP, Python,
C#, etc., and each works slightly differently.

Using the basic binary format enables the native data structures to be built quickly for each language
without going through the hassle of first processing JSON.

Creating or Generating a Unique Key

Each JSON document should have a unique identifier. It is the _id key. It is similar to the primary key in
relational databases. This facilitates search for documents based on the unique identifier. An index is
automatically built on the unique identifier. It is your choice to either provide unique values yourself or have
the mongo shell generate the same.

Database
It is a collection of collections. In other words, it is like a container for collections. Data base is created first
time that your collection makes a reference to it. Database can be created on demand. Each database gets
its own set of files on the file system. A single MongoDB server can house several databases.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #4

Collection
A collection is analogous to a table of RDBMS. A collection is created on demand.

Collection is created the first time that you attempt to save a document that references it.

A collection exists within a single database. A collection holds several MongoDB documents.

A collection does not enforce a schema. This implies that documents within a collection can have different
fields.
Even if the documents within a collection have same fields, the order of the fields can be different.

Document
A document is analogous to a row/record/tuple in an RDBMS table.
A document has a dynamic schema. This implies that a document in a collection need not necessarily have
the same set of fields/key-value pairs.

Support for Dynamic Queries


MongoDB has extensive support for dynamic queries. Where in traditional RDBMS we have static data and
dynamic queries.
CouchDB, another document-oriented, schema-less NoSQL database and MongoDB's biggest competitor
works on quite the reverse philosophy. It has support for dynamic data and static queries.

A collection "students" containing 3 documents.

6.2.4 Storing Binary Data


BinData is a BSON data type for a binary byte array. MongoDB objects are typically limited to 4MB in
size. To deal with this, files are "chunked" into multiple objects that are less than 4MB each. This has the
added advantage of letting us efficiently retrieve a specific range of the given file.

MongoDB provides GridFS to support the storage of binary data. This usually suffices for photographs
(such as a profile picture) or small audio clips. However, if one wishes to store movie clips, MongoDB has
another solution.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #5

It stores the fs.files (metadata) (data about data along with the context information) in a collection called "file".

It then breaks the data into small pieces called fs.chunks(chunks) and stores it in the "chunks" collection.
This process takes care about the need for easy scalability.

GridFS used in In MongoDB, to storing files larger than 16 MB.

• In some situations, storing large files may be more efficient in a MongoDB database than on a
system-level filesystem.
• If your filesystem limits the number of files in a directory, you can use GridFS to store as many files
as needed.
• When you want to access information from portions of large files without having to load whole files
into memory, you can use GridFS to recall sections of files without reading the entire file into
memory.
• When you want to keep your files and metadata automatically synced and deployed across a
number of systems and facilities, you can use GridFS. When using geographically distributed
replica sets, MongoDB can distribute files and their metadata automatically to a number of
mongod instances and facilities.

6.2.5 Replication
MongoDB provides data redundancy and high availability.
Replication provides redundancy and increases data availability. With multiple copies of data on different
database servers, replication provides a level of fault tolerance against the loss of a single database server.
In some cases, replication can provide increased read capacity as clients can send read operations to
different servers. Maintaining copies of data in different data centers can increase data locality and
availability for distributed applications. It helps to recover from hardware failure and service interruptions
You can also maintain additional copies for dedicated purposes, such as disaster recovery, reporting, or backup.
A replica set is a group of mongod instances that maintain the same data set. A replica set contains
several data bearing nodes and optionally one arbiter node. In MongoDB, the replica set has a single
primary and several secondaries.

Each write request from the client is directed to the primary. The primary logs all write requests into its
Oplog (operations log). The Oplog is then used by the secondary replica members to synchronize their
data. This way there is strict adherence to consistency.

In the above, The clients usually read from the primary. However, the client can also specify a read
preference that will then direct the read operations to the secondary.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #6

6.2.6 Sharding
Sharding is akin to horizontal scaling. It means that the large data set is divided and distributed over
multiple servers or shards. Each shard is an independent database and collectively they would constitute a
logical database.

No Sharding Database Sharding Database

Sharding is of two types 1. Hash sharding 2. Range sharding

Basically, the concept of sharding comes when we need to


deal with larger datasets.
This feature helps to distribute this problematic data to
multiple MongoDB instances.
The collections in the MongoDB which has a larger size
Process of sharding in mongoDB are distributed in multiple collections. These collections
are called “shards”. Shards are implemented by clusters.
The prime advantages of sharding are as follows:
1. Sharding reduces the amount of data that each shard needs to store and manage.
For example, if the dataset was 1TB in size and we were to distribute this over four shards, each shard
would house just 256 GB data. In the above, As the cluster grows, the amount of data that each shard will
store and manage will decrease.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #7

2. Sharding reduces the number of operations that each shard handles.


For e.g, if we were to insert data, the application needs to access only that shard which houses that data.

6.2.7 Updating Information In-Place

MongoDB updates the information in-place. This implies that it updates the data wherever it is available. It
does not allocate separate space and the indexes remain unaltered.

MongoDB is all for lazy-writes. It writes to the disk once every second.
Reading and writing to disk is a slow operation as compared to reading and writing from memory.
The fewer the reads and writes that we perform to the disk, the better is the performance.

This makes MongoDB faster than its other competitors who write almost immediately to the disk.
However, there is a tradeoff. MongoDB makes no guarantee that data will be stored safely on the disk.

6.3 TERMS USED IN RDBMS AND MONGODB

MySQL Oracle MongoDB


Database Server MySqld Oracle Mongod
Database Client MySql SQL Plus mongo

6.3.1 Create Database


Action
syntax : use DATABASE Name > use myDB;
e.g To create a database by the name "myDB" switched to db myDB
syntax is use myDB >
The "use" command is used to create a database in MongoDB.
If the database does not exist a new one will be created.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #8

To check your database exists or not , type the command at the MongoDB shell: db
run db in the shell to tell you the name of the active database
> db;
myDB
>

To get a list of all databases that already exist in the system, - show dbs
> show dbs;
admin (empty)
local 0.078GB
test 0.078GB
>
Note: Newly created database, "myDB" does not show up in the list above. In order to list out the database
needs to have at least one document in the list.
The default database in MongoDB is test. If one does not create any database, all collections are by default
stored in the test database.
6.3.2 Drop Database
syntax: db.dropDatabase();
e.g - To drop the database, "myDB", first ensure that you are currently placed in "myDB" database and then use
the db.dropDatabase() command to drop the database.

use myDB;
db.dropDatabase();
Confirm if the database "myDB" has been dropped.

>db.dropDatabase();
{ "dropped" : "myDB", "ok" : 1 }
>
Note: If no database is selected, the default database "test" is dropped.

6.4 DATA TYPES IN MONGODB


MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #9

The following are various data types in MongoDB.


DATATYPE Description
String This is the most commonly used datatype to store the data. String in MongoDB must be
UTF-8 valid. https://www.w3schools.com/charsets/ref_html_utf8.asp
Integer This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon
your server.
Boolean: Boolean: This type is used to store a boolean (true/ false) value.
Double This type is used to store floating point values.
Min/Max This type is used to compare a value against the lowest and highest BSON elements.
Keys
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 to it.
Object ID This datatype is used to store the document’s ID.
Binary data This datatype is used to store binary data
Code This datatype is used to store JavaScript code into the document.
Regular This datatype is used to store regular expression
expression

A few commands worth looking at are as follows


To report the name of the current database:
>db
test
>

To display the list of databases:


>show dbs
admin (empty)
local 0.078GB
myDB1 0.078GB
>
To switch to a new database, for example, myDB1:
>use myDB1
switched to db myDB1
>
Create collections - implicitly
db.mycollectn1.insert({
“name”: “Prudhvi”
})
Above command will create a collection with name “mycollectn1” if it doesn’t exist. It will be created implicitly.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #10

> db.employee.insert( Here employee is collection created, in which one


{ "Eid" : 1, document is also created with key:value pairs
"EmployeeName" : "Murali" EID,EmployeeName
}
)
WriteResult({ "nInserted" : 1 })

> show collections Displaying the collections with in the database


employee

> db.employee.find() Displaying the documents in the collections


{ "_id" : ObjectId("5d65cf65bf9e24aef7bc0e38"),
"Eid" : 1, "EmployeeName" : "Murali" }

> db.employee.find().pretty() Displaying the documents in the collections in


{ formatted way
"_id" : ObjectId("5d65cf65bf9e24aef7bc0e38"),
"Eid" : 1,
"EmployeeName" : "Murali"
}
>

Create collections - Explicitly

We can also create collection explicitly using “createCollection()” command.


db.createCollection(name, options)
>use myDB1
switched to myDB1

>db.createCollection(“mongodb”, db.createCollection(“mongodb”);
{ capped:true, size:1000000, max:2}) { “ok” : 1 }
{ “ok” : 1 }
Here we can create collection with options as well as with out options
To display the list of collections (tables) in the current database
> show collections
system.indexes
system.js
>
To display the current version of the MongoDB server:
> db.version()
3.4.3
>
To display the statistics that reflect the use state of a database:
> db.stats() > db.stats()
{ {
"db" : "myDB1", "db" : "mydb",
"collections" : 3, "collections" : 1,
"objects" : 6, "views" : 0,
"avgObjSize" : 122.66666666667, "objects" : 1,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #11

"dataSize" : 736, "avgObjSize" : 60,


"storageSize" : 24576, "dataSize" : 60,
"numExtents" : 3, "storageSize" : 16384,
"indexes" : 1, "numExtents" : 0,
"indexsize" : 8176, "indexes" : 1,
"fileSize" : 67108864, "indexSize" : 16384,
"nsSizeMB" : 16, "ok" : 1
"dataFileVersion" : { }
"major" : 4, >
"minor" : 5
},
"extentFreeList" : {
"num" : 14,
"totalSize" : 974848
},
"ok" : 1
}
>
Type in db.help() in the MongoDB client to get a list of commands:
> db.help();
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls
db.runCommand(...) ]
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyoatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : capped : max : ... } )
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db db.dropDatabase()
db.eval(func, args) run code server-side
db.fsyncLock() flush data to disk arid lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db[ cname'] or db.cname
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getMongo() get the server connection object
db.getMongo() .setSlaveOkO allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold db.getRepli cationlnfo()
db.getsiblingDB(name) get the db at the same server as this one
db.getwriteConcern() - returns the write concern used for any operations on this db, inherited from
server object if set
db.hostinfo() get details about the server's host
db.isMaster() check replica primary status
db.killop(opid) kills the current operation in the db
db.listcommands() lists all the db commands
db.loadserverscriptso loads all the scripts in db.system.js
db.logout()
db.printcollectionstats()
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #12

db.printReplicationInfo()
db.printShardi ngStatus()
db.printslaveReplicationInfo()
db.dropuser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdobj) run a database command, if cmdobj is a string, turns it into { cmdobj : 1 }
db.serverStatus()
db.setProfilingLevel(level,<slowms>) 0=off l^slow 2=all
db.setwriteConcern( <write concern doc> ) - sets the write concern for w rites to the db
db.unsetWriteConcern( <write concern doc> ) - unsets the write concern f or writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server

****
Consider a table "Students" with the following columns:
1. StudRollNo 2. StudName 3. Grade 4. Hobbies 5. DOJ
Before we get into the details of CRUD operations in MongoDB, let us look at how the statements are written
in RDBMS and MongoDB.

SQL Statement Mongo Query Language Statement


CREATE TABLE USERS (a Number, b Implicit or use MongoDB::createCollection().
Number)
INSERT INTO USERS VALUES(1,1) $db->users->insert(array("a" => 1, "b" => 1));
SELECT a,b FROM users $db->users->find(array(), array("a" => 1, "b" => 1));
SELECT * FROM users WHERE age=33 $db->users->find(array("age" => 33));
SELECT a,b FROM users WHERE age=33 $db->users->find(array("age" => 33), array("a" => 1, "b" => 1))-
ORDER BY name >sort(array("name" => 1));
SELECT * FROM users WHERE age>33 $db->users->find(array("age" => array('$gt' => 33)));
SELECT * FROM users WHERE age<33 $db-users->find(array("age" => array('$lt' => 33)));
SELECT * FROM users WHERE name $db->users->find(array("name" => new
LIKE "%Krishna%" MongoRegex("/Krishna/")));
SELECT * FROM users WHERE age>33 $db->users->find(array("age" => array('$gt' => 33, '$lte' =>
AND age<=40 40)));
SELECT * FROM users ORDER BY name $db->users->find()->sort(array("name" => -1));
DESC
CREATE INDEX myindexname ON $db->users->ensureIndex(array("name" => 1));
users(name)
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #13

CREATE INDEX myindexname ON $db->users->ensureIndex(array("name" => 1, "ts" => -1));


users(name,ts DESC)
SELECT * FROM users WHERE a=1 and $db->users->find(array("a" => 1, "b" => "q"));
b='q'
SELECT * FROM users LIMIT 20, 10 $db->users->find()->limit(10)->skip(20);
SELECT * FROM users WHERE a=1 or $db->users->find(array('$or' => array(array("a" => 1), array("b"
b=2 => 2))));
SELECT * FROM users LIMIT 1 $db→users→find()→limit(1);
EXPLAIN SELECT * FROM users $db->users->find(array("z" => 3))→explain()
WHERE z=3
SELECT DISTINCT last_name FROM $db->command(array("distinct" => "users", "key" =>
users "last_name"));
SELECT COUNT(*y) FROM users $db→users→count();
SELECT COUNT(*y) FROM users $db→users→count();
SELECT COUNT(*y) FROM users where $db->users->find(array("age" => array('$gt' => 30)))→count();
AGE > 30
SELECT COUNT(AGE) from users $db->users->find(array("age" => array('$exists' =>
true)))→count();
UPDATE users SET a=1 WHERE b='q' $db->users->update(array("b" => "q"), array('$set' => array("a"
=> 1)));
UPDATE users SET a=a+2 WHERE b='q' $db->users->update(array("b" => "q"), array('$inc' => array("a"
=> 2)));
UPDATE users SET a=a+2 WHERE b='q' $db->users->update(array("b" => "q"), array('$inc' => array("a"
=> 2)));
DELETE FROM users WHERE z="abc" $db->users->remove(array("z" => "abc"));

6.5 MONGODB QUERY LANGUAGE


CRUD (Create, Read Update, and Delete) operations in MongoDB

Create—> Creation of data is done using insert() or update() or save() method.

Read —> Reading the data is performed using the find() method.

Update —> Update to data is accomplished using the update() method with UPSERT set to false.

Delete —> a document is Deleted using the remove() method.


MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #14

Objective: What is it that we are trying to achieve here?


Input : What is the input that has been given to us to act upon?
Act : The actual statement/command to accomplish the task at hand.
Outcome : The result/output as a consequence of executing the statement.
At few places we have also provided the equivalent in RDBMS such as Oracle.

Objective: To create a collection by the name "Person". Let us take a look at the collection list prior to the
creation of the new collection "Person":
> show collections
Students
food

Act: The statement to create the collection is db.createCollection ("Person")


Outcome: Below is the collection list after the creation of the new collection "Person"
>show collections;
Person
Students
food
>

Objective: To drop a collection by the name "food". Take a look at the current collection list:
Person
Students
food

Act: The statement to drop the collection is db.food.drop();


>db.food.drop()
true
>
Outcome: The collection list after the execution of the statement is as follows:
6.5.1 Insert Method
We now explain the syntax of insert method.
db.student.insert( ←Collection
{
rollno: 17301, ←Field: value
name:Murali ←Field: value
age: 23, ←Field: value
contactNo:9848199299, ←Field: value
e-mail:murali@gmail.com ←Field: value
}
)

Objective: To create a collection by the name "student" and insert documents.


> use mydb
switched to db mydb
Input: Check the list of existing collections.

> show collections;


employee
food
person

> db.createCollection("student");
{ "ok" : 1 }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #15

> show collections;


employee
food
person
student

To Store the document(s) in to collection we had three functions 1.insert() 2.update() 3. save()
1. insert() - insert a fresh document in to collection
2. update() - insert a fresh document in to collection if document was not found with upsert:true option
if document is found we can update the document fields
3. save() - insert a fresh document in to collection if document was not found with _id
if document _id is found we can replace the document
1. insert() - insert a fresh document in to collection student
> db.student.insert( { "rollno":17301, "name":"Murali", "age":23, "contactno":9848929299,"e-
mail":"muralivadlamudi@gmail.com"})
WriteResult({ "nInserted" : 1 })

Check whether document is stored in student collection


> db.student.find()
{ "_id" : ObjectId("5d65d54557c4c2d917f18bb3"), "rollno" : 17301, "name" : "Murali", "age" : 23, "contactno"
: 9848929299, "e-mail" : "muralivadlamudi@gmail.com" }

Check whether document is stored in student collection – Formatted way


> db.student.find().pretty()
{
"_id" : ObjectId("5d65d54557c4c2d917f18bb3"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848929299,
"e-mail" : "muralivadlamudi@gmail.com"
}

Insert another document in to collection – student


> db.student.insert( { "rollno":17302, "name":"Krishna", "age":23, "contactno":9848111299,"e-
mail":"krishna@gmail.com"})
WriteResult({ "nInserted" : 1 })
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #16

> db.student.find()
{ "_id" : ObjectId("5d65d54557c4c2d917f18bb3"), "rollno" : 17301, "name" : "Murali", "age" : 23, "contactno"
: 9848929299, "e-mail" : "muralivadlamudi@gmail.com" }
{ "_id" : ObjectId("5d65d89457c4c2d917f18bb4"), "rollno" : 17302, "name" : "Krishna", "age" : 23,
"contactno" : 9848111299, "e-mail" : "krishna@gmail.com" }

> db.student.find().pretty()
{
"_id" : ObjectId("5d65d54557c4c2d917f18bb3"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848929299,
"e-mail" : "muralivadlamudi@gmail.com"
}
{
"_id" : ObjectId("5d65d89457c4c2d917f18bb4"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848111299,
"e-mail" : "krishna@gmail.com"
}
>
2. update() - insert a fresh document in to collection if document was not found with upsert1:true option
if document is found we can update the document fields
Here we are searching for a document, if that is found we update it else we insert the document into
collection
> db.student.update({"rollno":17303,"name":"Vamsi","age":23},{$set:{"contactno":9848811299,"e-
mail":"vamsi@gmail.com"}},{upsert:true});
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5d65de78386852e892928055")
})
upsert – is a boolean and Optional. If set to true, creates a new document when no document matches the
query criteria. The default value is false, which does not insert a new document when no match is found.

> db.student.find().pretty()
{
"_id" : ObjectId("5d65dd2ade1660c49412a60d"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com"
}
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
1 upsert(Noun) An operation that inserts rows into a database table (if they do not already exist) or updates them
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #17

"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com"
}
{
"_id" : ObjectId("5d65de78386852e892928055"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 9848811299,
"e-mail" : "vamsi@gmail.com"
}
>
In this example Name: Vamsi is not found hence record is inserted
Now we try to update Vamsi phone number to 7988811299 as vamsi document already exists

> db.student.update({"rollno":17303,"name":"Vamsi","age":23},{$set:{"contactno":7988811299}},
{upsert:true});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.student.find().pretty()
{
"_id" : ObjectId("5d65dd2ade1660c49412a60d"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com"
}
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com"
}
{
"_id" : ObjectId("5d65de78386852e892928055"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com"
}
>

3. save() - insert a fresh document in to collection if document was not found with _id
if document _id is found we can replace the document

>db.student.save({"rollno":17304,"name":"Sudha","age":21,"contactno":6398193339,"e-
mail":"sudha@gmail.com"})
WriteResult({ "nInserted" : 1 })
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #18

> db.student.find().pretty()
{
"_id" : ObjectId("5d65dd2ade1660c49412a60d"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com"
}
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com"
}
{
"_id" : ObjectId("5d65de78386852e892928055"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com"
}
{
"_id" : ObjectId("5d65e206de1660c49412a60f"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com"
}
>

Search for the documents - find() (Search for the document based on criteria)
e.g – Search for document in collection student with rollno- 17302

>db.student.find({rollno:17302});
{ "_id" : ObjectId("5d65dd58de1660c49412a60e"), "rollno" : 17302, "name" : "Krishna", "age" : 23,
"contactno" : 9848191299, "e-mail" : "krishna@gmail.com" }
> db.student.find({rollno:17302}).pretty()
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com"
}
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #19

e.g – Search for document in collection student with name- Sudha

> db.student.find({"name":"sudha"}).pretty()

we did not got document as name is quoted as “sudha” instead of “Sudha”

> db.student.find({"name":"Sudha"}).pretty()
{
"_id" : ObjectId("5d65e206de1660c49412a60f"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com"
}
>
Search for the document with out criteria projecting the specified fields
Now list out the specified fields in the document as per our choice
selecting rollno,name,contactno only
syntax : db.student.find({},{rollno:1,name:1,contactno:1}).pretty()
Here we set rollno:1,name:1,contactno:1

1 indicate that field should display (true)


0 indicate that field should not display (false)

e.g
> db.student.find({},{rollno:1,name:1,contactno:1}).pretty()
{
"_id" : ObjectId("5d65dd2ade1660c49412a60d"),
"rollno" : 17301,
"name" : "Murali",
"contactno" : 9848912299
}
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"contactno" : 9848191299
}
{
"_id" : ObjectId("5d65de78386852e892928055"),
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299
}
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #20

{
"_id" : ObjectId("5d65e206de1660c49412a60f"),
"rollno" : 17304,
"name" : "Sudha",
"contactno" : 6398193339
}

with out _id number


> db.student.find({},{rollno:1,name:1,contactno:1,_id=0}).pretty()
2019-08-28T02:30:10.219+0000 E QUERY [thread1] SyntaxError: missing : after property id @(shell):1:51

> db.student.find({},{rollno:1,name:1,contactno:1,_id:0}).pretty()
{ "rollno" : 17301, "name" : "Murali", "contactno" : 9848912299 }
{ "rollno" : 17302, "name" : "Krishna", "contactno" : 9848191299 }
{ "name" : "Vamsi", "rollno" : 17303, "contactno" : 7988811299 }
{ "rollno" : 17304, "name" : "Sudha", "contactno" : 6398193339 }
>
Relational Operators
Name Description
$eq Matches values that are equal to a specified value.
$gt Matches values that are greater than a specified value.
$gte Matches values that are greater than or equal to a specified value.
$lt Matches values that are less than a specified value.
$lte Matches values that are less than or equal to a specified value.
$ne Matches all values that are not equal to a specified value.
$in Matches any of the values specified in an array. (similar to unix shell - for variable in list)
$nin Matches none of the values specified in an array. ( not in list)

Adding a new field to an existing document – update method

Here’s the MongoDB shell syntax for update():


db.collection.update(criteria, objNew, upsert, multi )
criteria – Query which selects the record to update;
objNew – Updated object or $ operators (e.g., $inc) which manipulate the object
upsert – if this should be an “upsert” operation; that is, if the record(s) do not exist, insert one.
*Upsert only inserts a single document.
multi – Indicates if all documents matching criteria should be updated rather than just one.
Can be useful with the $ operators below
> db.student.update({},{$set:{creditscore:"A"}},{multi:true});
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })

In the above example we use empty criteria {}, as we need add field all records

> db.student.find().pretty()
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #21

"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "A"
}
{
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A"
}
>
Now change creditscore to person named Vamsi to “B”
> db.student.update({name:"Vamsi"},{$set:{creditscore:"B"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find().pretty()
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A"
}
{
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #22

"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B"
}
{
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A"
}
>
Here credit score for Vamsi has been changed to – B

Other Options for update


1. collection.updateOne()
2. collection.updateMany()
3. collection.findOneAndUpdate()
4. collection.replaceOne()
5. collection.findOneAndReplace()
6. collection.findAndModify()

1. collection.updateOne() This update statement updates the FIRST document that matches
the given filter. Filter is any criteria based on which we want to
update document
db.student.updateOne( This update commands use the
{ "age": 18 }, age =18 as a filter (match the query) in the collection “students”.
{
$set: { "canVote": "True" }, $set operator (called as update operators) updates the value of the
canVote to True.
} To update multiple parameters, parameters must be separated by a
) comma (,).
e.g.: $set: { "canVote": "True", “isAdult”=”Yes” },

2. collection.updateMany() This command is similar to db.collection.updateOne() but the only


difference is that it updates ALL the documents that match the
filter in the collection. (i.e to update multiple documents)
db.students.updateMany( Here, ALL the documents having age=18 get updated to canVote to
{ "age": 18 }, “True”.

{ We can also set file as {“age”:$ge 18}


$set: { "canVote": "True" }, here age greater than or equal to 18 works
}
)
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #23

3. collection.findOneAndUpdate() This updates a single document based on the filter and other
criteria. The simple syntax is:
db.collection.findOneAndUpdate(filter, update, options)
db.collection.findOneAndUpdate( Here, we have some additional parameters:
<filter>,
<update>,
{ Projection: Optional. It denotes A subset of fields to return. If you
projection: <document>, want to return all fields in the returned document, do not include this
parameter.
sort: <document>, Sort: Specifies a sorting order for the documents matched by the
filter.
returnNewDocument: <boolean> returnNewDocument: It returns the updated document instead of
} the original document when set to true.
)
4. collection.replaceOne() This command replaces at most a single document (replace an
existing document) that matches a specified filter even though
multiple documents may match the specified filter.
db.collection.replaceOne(
<filter>,
<replacement>,
{ Here, upsert is of boolean type. upsert can be either true or false.
upsert: <boolean>, If set to true, it creates a new document when no document
matches the query criteria.
writeConcern: <document> The default value is false, which does not insert a new document
} when no match is found.
)
5.collection.findOneAndReplace() This command modifies and replaces a single document based on
the filter and sort criteria.
db.collection.findOneAndReplace(
<filter>,
<replacement>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnNewDocument: <boolean>
}
)

6. collection.findAndModify() This command modifies and returns a single document. But the
key point to be noted here is that by default, the returned document
does not include the modifications made on the update. To return
the document with the modifications made on the update, use the
new option.
db.collection.findAndModify({
query: <document>,
sort: <document>,
remove: <boolean>,
update: <document>,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #24

new: <boolean>,
fields: <document>,
upsert: <boolean>,
bypassDocumentValidation:
<boolean>,
writeConcern: <document>
});

Adding a New field “location” to the collection student


>db.student.update({},{$set:{location:"Vijayawada"}},{multi:true});
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
> db.student.find().pretty()
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B",
"location" : "Vijayawada"
}
{
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada"
}
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #25

Here we inserted field as “hobbies” and set all as “sports”


> db.student.update({},{$set:{hobbies:"sports"}},{multi:true});
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })

> db.student.find().pretty()
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17306,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #26

"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
>

MongoDB delete documents – how to delete documents in the collection.


There are many ways in MongoDB from which we can delete documents in the collection:

1. collection.remove()
2. collection.deleteOne() – New in version 3.2.
3. collection.deleteMany() – New in version 3.2.
4. collection.findOneAndDelete()

1. collection.remove() syntax: db.collection.remove()

This method deletes a single document or all documents from the collection that match a specified filter.
db.collection.remove() method (remove method) works on two parameters.

Deletion criteria: Condition on which document has to be deleted. (Match a deletion criteria)
JustOne: To remove only one document when set to true or 1.

db.collection_name.remove (DELETION_CRITERIA, JustOne)

To remove all documents that match a condition:

>db.student.remove({age : 18 } )

This command will delete all the documents from the ‘student’ collection where ‘age’ field is equal to ‘18’

To remove a single document that matches a condition:

If you want to remove a single document that matches a given condition, then use the following syntax.

>db.student.remove({age : 18 }, 1 )

Note:To delete all the documents from the collection (i.e. pass an empty filter without any condition)

>db.student.remove({})
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #27

2. db.collection.deleteOne()
This method deletes at most a single document that matches a specified filter even though multiple
documents may match the specified filter. (MongoDB in v3.2)

Syntax: db.student.deleteOne({location : "Vijayawada" } )


Note:This is similar to db.collection.remove() method with the <justOne> parameter set to true or 1.

3. db.collection.deleteMany()

This method deletes all documents that match a specified filter. (MongoDB v3.2)

db.student.deleteMany({age : 17})

This command will delete all the documents from the ‘student’ collection where ‘age’ field is equal to ‘17’.

4) db.collection.findOneAndDelete()
This method deletes a single document based on the filter and sort criteria, returning the deleted document.

Syntax: db.collection.findOneAndDelete(filter, options)

db.collection.findOneAndDelete(
<filter>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
}
)

Removing the existing field from an existing document – remove method

Objective: To remove the field "Location" with value "Nellore" in the document of "Students"
collection.

>db.student.update({“name”:”Pravallika”},{$unset:{Location:”Nellore”});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
Outcome: Confirm if the stated field ("Location") has been dropped from the document of the "Students"
collection.

6.5.5 Finding Documents based on Search Criteria - Find Method

The syntax of find method is as follows:


db.students.find( ← Collection
{Age: {$gt 18}}, Selection Criteria
{RollNo:1 ,Age:1,_id:1} ← Projection
) ← Cursor Modifier
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #28

Task1: Find the document wherein the “Name” has value "Anamika" in students collection
>db.student.find({“name”: "Anamika"}).pretty(); output:{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17307,
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
>
Task2: Find the document wherein the “creditscore” has value "A" in students collection
>db.student.find({“creditscore”: "A"}).pretty(); Output:
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
...
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17306,
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
>

Task3: Find the document wherein the “creditscore” has value not equal to "A" in students collection
>db.student.find({“creditscore”: {$ne:"A"}}).pretty(); Output:
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #29

"creditscore" : "B",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}

>

Regular Expression in MongoDB


Task4: Find the document wherein the “hobbies” are either "chess" or "acting" in students collection
>db.student.find({“hobbies”: {$in: Output:
["chess","acting"]}}).pretty(); {
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
Here - and operation by "age" : 21,
{key: {$in:[value,value]}} "contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
{
"_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
>

Task5: Find the document wherein the “hobbies” are neither "chess" nor "acting" in students
collection
>db.student.find({“hobbies”: {$nin: Output:
["chess","acting"]}}).pretty(); {
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
Here - and operation by "age" : 23,
{ key: {$nin:[value,value]}} "contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #30

"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
...
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17307,
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
>

AND condition
Task6: Find the document wherein the “hobbies” is sports and "location" Vijayawada in students collection
Output:
>db.student.find({"hobbies":"sports","location":"Vijayawa {
da"}).pretty(); "_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
Here - and operation by separating simply by comma "contactno" : 9848912299,
key:value,key:value "e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
hobbies:"sports",location:"Vijayawada" "hobbies" : "sports"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
...
>

Task7: To find documents from the Students collection where the name begins with "S"
Output:
>db.student.find({"name":/^S/}).pretty(); {
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
/^S/ - indicate string beginning with S in regular "age" : 21,
expression "contactno" : 6398193339,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #31

"e-mail" : "sudha@gmail.com",
Note: "creditscore" : "A",
"location" : "Vijayawada",
Here “//” specifies that our search criteria is within "hobbies" : "sports"
these delimiters }
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
>
Task8: To find documents from the Students collection where the name end with "i"
Output:
>db.student.find({"name":/i$/}).pretty(); {
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
/i$/ - indicate string ending with i in regular expression "e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B",
"location" : "Vijayawada",
"hobbies" : "sports"
}
>

Task9: To find documents from the Students collection where the name has an "v" in any position
db.student.find({name:/v/}) .pretty (); Output:
OR {
db.student.find({name:/.*v.*/}) .prettyO; "_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
OR "name" : "Pravallika",
db.student.find({name:{$regex:"v"}}).pretty(); "age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #32

Using regex
Task10: To find documents from the Students collection where the name begins with "S" - Using regex
Output:
>db.student.find({name:{$regex:"^S"}}).pretty(); {
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
>
Task11: To find documents from the Students collection where the name end with "i"- using regex
Output:
>db.student.find({"name":{$regex:"i$"}}).pretty(); {
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B",
"location" : "Vijayawada",
"hobbies" : "sports"
}
>

Count, Limit, Sort, and Skip

1. Count - Count all document from the collection


Task1: Count the number of the documents in the student collection.

>db.student.count();
7
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #33

Task2: Count the number of the documents in the student collection where location is Viajaywada.

>db.student.count({location:”Vijayawada”});
4

Task3: retrieve the first 2 documents in the student collection where location is Viajaywada.

2.limit
Syntax: >db.COLLECTION_name.find().limit(NUMBER)

>db.student.find({location:”Vijayawada”}).limit(2).pretty()
output:
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}

3. Skip
skip() which also accepts number type argument and is used to skip the number of documents.

Syntax: db.COLLECTION_name.find().limit(NUMBER).skip(NUMBER)
Note: The default value in skip() method is 0 (zero)

Task1: Skip first 4 documents from students collection

>db.student.find().skip(4).pretty()

out put:
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17307,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #34

"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
>
Task2: Skip first 4 documents from students collection limit to 2 documents
>db.student.find().skip(4).limit(2).pretty()

out put:
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17307,
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
>
Here we got documents from 5 th onwards by skip(4) and listing only 2 documents as limit(2)

Task3:To display the last 2 records from the student collection.


db.student.find().pretty().skip(db.student.count()-2)
Here skip(db.student.count()-2) display last two docuements

>db.student.find().pretty().skip(db.student.count()-2)
{
"_id" : ObjectId("5d69db5b027d622d21d17ada"),
"rollno" : 17306,
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #35

"hobbies" : "chess",
"name" : "Pravallika"
}
{
"_id" : ObjectId("5d69dbe1027d622d21d17adb"),
"rollno" : 17307,
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports",
"name" : "Anamika"
}
>
Task 4: To retrieve the third, fourth, and fifth document from the student collection.
db.student.find().pretty().skip(2).limit(3)
Here we are skipping first two documents with skip(2), then displaying next three documents by limit(3)

>db.student.find().pretty().skip(2).limit(3)
{
"_id" : ObjectId("5d69d6e18cd7279cccb0f494"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69d792027d622d21d17ad8"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69dad7027d622d21d17ad9"),
"rollno" : 17305,
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting",
"name" : "Snigdha"
}
>

4. Sort
Task 1: To sort the documents from the Student collection name in ascending order
db.student.find().sort({name:1}).pretty();
{
"_id" : ObjectId("5d69dbe1027d622d21d17adb"),
"rollno" : 17307,
"name" : "Anamika",
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #36

"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69d6bf027d622d21d17ad7"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848111299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848929299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69db5b027d622d21d17ada"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
{
"_id" : ObjectId("5d69dad7027d622d21d17ad9"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}

Task2: To sort the documents from the student collection first on name in ascending order and then on
Hobbies in desending order.
db.student.find().sort({name: 1, hobbies: -1}).pretty()
Here documents are sorted first on name in ascending order(1) then hobbies in descending order (-1)
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #37

>db.student.find().sort({name: 1, hobbies: -1}).pretty()


{
"_id" : ObjectId("5d69dbe1027d622d21d17adb"),
"rollno" : 17307,
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports",
"name" : "Anamika"
}
{
"_id" : ObjectId("5d69d6bf027d622d21d17ad7"),
"rollno" : 17302,
"age" : 23,
"contactno" : 9848111299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports",
"name" : "Krishna"
}
{
"_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"),
"rollno" : 17301,
"age" : 23,
"contactno" : 9848929299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports",
"name" : "Murali"
}
{
"_id" : ObjectId("5d69db5b027d622d21d17ada"),
"rollno" : 17306,
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess",
"name" : "Pravallika"
}
{
"_id" : ObjectId("5d69dad7027d622d21d17ad9"),
"rollno" : 17305,
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting",
"name" : "Snigdha"
}
{
"_id" : ObjectId("5d69d792027d622d21d17ad8"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #38

"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69d6e18cd7279cccb0f494"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
>
Task3: To sort the documents from student collection by name and skip the first two documents from sorted list
Syntax : db.student.find().skip(2).pretty().sort({name:1})

> db.student.find().skip(2).pretty().sort({name:1})
{
"_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"),
"rollno" : 17301,
"age" : 23,
"contactno" : 9848929299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports",
"name" : "Murali"
}
{
"_id" : ObSyntaxjectId("5d69db5b027d622d21d17ada"),
"rollno" : 17306,
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess",
"name" : "Pravallika"
}
{
"_id" : ObjectId("5d69dad7027d622d21d17ad9"),
"rollno" : 17305,
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting",
"name" : "Snigdha"
}
{
"_id" : ObjectId("5d69d792027d622d21d17ad8"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #39

{
"_id" : ObjectId("5d69d6e18cd7279cccb0f494"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
>

NULL values
To add new field with null value in existing documents of students (either to specified criteria or to all)
A NULL is a missing or unknown value. When null value is placed for a field it indicate that we did not know
value or value missing.
But we can always update the value NULL later.
Existing documents in student collection
> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali" }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna" }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports" }
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports" }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha" }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika" }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika" }
>

Task1: We add new field “disability” and assign null to all documents

>db.student.update({},{$set:{disability:null}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali", "disability" : null } ←Here one document is updated as we specified update
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #40

{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-


mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna" }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports" }
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports" }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha" }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika" }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika" }
>

←Here one document is updated as we specified update, if we need to update all documents we need to specify
updateMany()

db.student.updateMany({},{$set:{disability:null}})
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 6 }

> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali", "disability" : null }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna", "disability" : null }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports", "disability" : null }
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports", "disability" : null }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha", "disability" : null }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika", "disability" : null }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika", "disability" : null }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #41

Task3 : assign a value:”Physically challenged” to field disability for a document whose name is Vamsi

db.student.update({name:”Vamsi”},{$set:{disability:”Physically Challenged”}})

>db.student.update({name:"Vamsi"},{$set:{disability:"Physically Challenged"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali", "disability" : null }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna", "disability" : null }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports", "disability" : "Physically Challenged" } ←Here one document is updated
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports", "disability" : null }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha", "disability" : null }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika", "disability" : null }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika", "disability" : null }
>

Task4: Remove the disability field from student collection where value is assigned “Physically
Challenged”

>db.student.update({name:"Vamsi"},{$unset:{disability:"Physically Challenged"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali", "disability" : null }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna", "disability" : null }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports" } ←Here one document is updated and field disability is deleted
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports", "disability" : null }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha", "disability" : null }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #42

{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-


mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika", "disability" : null }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika", "disability" : null }
>

Task5: Remove the disability field from student collection


> db.student.updateMany({},{$unset:{disability:null}})
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 6 }

> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali" }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna" }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports" }
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports" }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha" }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika" }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika" }
>
Creation of bulk documents in to collection at one stretch
Step1: Create a variable at mongo prompt
>var variable =
[
{ key:value,
key:value,
key:value
},
{ key:value,
key:value,
key:value
},
{ key:value,
key:value,
key:value
},
];
after execution you will get > prompt
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #43

Step2: Assign values in to a collection in current database


>db.collectionname.insert(variable);
Here data stored in variable is inserted in to the collectionname specified as documents
>
Step3: display documents in collection
Now we are about create a variable emp at mongo prompt
example:
Step1: Create a variable (emp) at mongo prompt
> var emp =
... [
... {
... "eno":1001,
... "ename":"Murali",
... "age":30,
... "qual":"M.Sc(Maths)",
... "role":"Lecturer",
... "dept":"Mathematics",
... "exp":7,
... "basic":12000
... },
... {
... "eno":1002,
... "ename":"Krishna",
... "age":33,
... "qual":"M.Sc(Maths)",
... "role":"Lecturer",
... "dept":"Mathematics",
... "exp":5,
... "basic":10000
... },
... {
... "eno":1003,
... "ename":"Vamsi",
... "age":33,
... "qual":"M.Sc(Physics)",
... "role":"Lecturer",
... "dept":"Physics",
... "exp":6,
... "basic":11000
... },
... {
... "eno":1004,
... "ename":"Sai Krishna",
... "age":33,
... "qual":"M.Sc(Computers)",
... "role":"Lecturer",
... "dept":"Computers",
... "exp":8,
... "basic":12000
... },
... {
... "eno":1005,
... "ename":"Snigdha",
... "age":33,
... "qual":"MCA",
... "role":"Lecturer",
... "dept":"Computers",
... "exp":8,
... "basic":12000
... },
... {
... "eno":1006,
... "ename":"Anamika",
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #44

... "age":32,
... "qual":"MCA",
... "role":"Lecturer",
... "dept":"Computers",
... "exp":8,
... "basic":12000
... },
... ];
>
Step2: Assign values in to a collection(employee)in current database

> db.employee.insert(emp);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 6,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
Step3: display documents in collection
> db.employee.find().pretty()
{
"_id" : ObjectId("5d7166896167c454bdec28fd"),
"eno" : 1001,
"ename" : "Murali",
"age" : 30,
"qual" : "M.Sc(Maths)",
"role" : "Lecturer",
"dept" : "Mathematics",
"exp" : 7,
"basic" : 12000
}
{
"_id" : ObjectId("5d7166896167c454bdec28fe"),
"eno" : 1002,
"ename" : "Krishna",
"age" : 33,
"qual" : "M.Sc(Maths)",
"role" : "Lecturer",
"dept" : "Mathematics",
"exp" : 5,
"basic" : 10000
}
{
"_id" : ObjectId("5d7166896167c454bdec28ff"),
"eno" : 1003,
"ename" : "Vamsi",
"age" : 33,
"qual" : "M.Sc(Physics)",
"role" : "Lecturer",
"dept" : "Physics",
"exp" : 6,
"basic" : 11000
}
{
"_id" : ObjectId("5d7166896167c454bdec2900"),
"eno" : 1004,
"ename" : "Sai Krishna",
"age" : 33,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #45

"qual" : "M.Sc(Computers)",
"role" : "Lecturer",
"dept" : "Computers",
"exp" : 8,
"basic" : 12000
}
{
"_id" : ObjectId("5d7166896167c454bdec2901"),
"eno" : 1005,
"ename" : "Snigdha",
"age" : 33,
"qual" : "MCA",
"role" : "Lecturer",
"dept" : "Computers",
"exp" : 8,
"basic" : 12000
}
{
"_id" : ObjectId("5d7166896167c454bdec2902"),
"eno" : 1006,
"ename" : "Anamika",
"age" : 32,
"qual" : "MCA",
"role" : "Lecturer",
"dept" : "Computers",
"exp" : 8,
"basic" : 12000
}
>
6.5.8 Arrays
These MongoDB data types stores array. A set of values are represented as an array. This data type can store
multiples of values and data types.

Task:Inserting multiple documents in to collection at a time


> db.food.insertMany([
... { fruits: ["banana","apple","cherry"]}, In this example, we are creating a collection named
... { fruits: ["orange","butterfruit","mango"]}, food,
... { fruits: ["pineapple","strawberry","grapes"]}, with five documents which contain fruits as array
... { fruits: ["banana","strawberry","grapes"]},
... { fruits: ["orange","grapes"]}
... ]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5d716e876167c454bdec2903"),
ObjectId("5d716e876167c454bdec2904"),
ObjectId("5d716e876167c454bdec2905"),
ObjectId("5d716e876167c454bdec2906"),
ObjectId("5d716e876167c454bdec2907")
]
}

Display the inserted documents


> db.food.find()
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "butterfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #46

Task: Find out the document fruit array contain "banana", "apple", "cherry" in food collection
>db.food.find({fruits:
["banana","apple","cherry"]}).pretty()
{
"_id" : ObjectId("5d716e876167c454bdec2903"),
"fruits" : [
"banana",
"apple",
"cherry"
]
}

To list documents from the "food" collection which has the "fruits" array having "banana", as an
element.
> db.food.find({fruits:"banana"})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana",
"apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana",
"strawbeery", "grapes" ] }
>

To find documents from the "food" collection where "grapes" is present in the 1st index position of the
"fruits" array
> db.food.find({"fruits.1":"grapes"}).pretty()
{
"_id" : ObjectId("5d716e876167c454bdec2907"),
"fruits" : [
"orange",
"grapes"
]
}
>

To find documents from the "food" collection where "grapes" is present in the 2nd index position of the
"fruits" array
> db.food.find({"fruits.2":"grapes"})
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple",
"strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery",
"grapes" ] }
>

To find and list documents from the "food" collection where the size of the array is two. The size
implies that the array holds only 2 values.
db.food.find({fruits:{$size:2}}) Here $size:2 identify
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] } documents with array
> size as 2
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #47

To find those documents from the "food" collection where the size of the array is three. Array with 3
values.
> db.food.find({fruits:{$size:3}})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "butterfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery", "grapes" ] }
>

To find the documents from array, whose size is 3 from the "food" collection and display the first two
elements
> db.food.find({fruits:{$size:3}},{fruits:{$slice:2}})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "butterfruit" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery" ] }
>

To find all documents from the "food" collection which have elements "orange" and "grapes" in the array
"fruits".
> db.food.find({fruits:["orange","grapes"]})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] }
>

To find those documents from the "food" collection which have the element "orange" in the 0 th index position in
the array "fruits".
> db.food.find({"fruits.0":"orange"})
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "butterfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] }
>

To find those documents from the "food" collection which have the element "orange" in the 0 th index position in
the array "fruits". Display two elements, starting with the element at 1st index position.
> db.food.find({"fruits.0":"orange"},{fruits:{$slice:[1,2]}})
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "butterfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "grapes" ] }
>

Update on the Array


To update the document with where “butterfruit” as 1st index and replace "jackfruit” in food array.
Before:
> db.food.find({"fruits.1":"butterfruit"} )
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "butterfruit", "mango" ] }
replacement process
> db.food.update({"fruits.1":"butterfruit"},{$set:{"fruits.1":"jackfruit"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

After action:
> db.food.find({"fruits.1":"jackfruit"} )
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "jackfruit", "mango" ] }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #48

To update the document where “grapes” is at 1st index position and replace new value in the "fruits"
array with “apple”
before
> db.food.find({"fruits.1":"grapes"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] }
after:
> db.food.update({"fruits.1":"grapes"},{$set:{"fruits.1":"apple"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
result:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
>

To update the documents where “apple” is at 1st index and replace new value pairs in the "fruits" array
with “an apple”
before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
Process:
> db.food.update({"fruits.1":"apple"},{$set:{"fruits.1":"an apple"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result
> db.food.find({"fruits.1":"an apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "an apple", "cherry" ] }
>
Here “apple” is replaced “an apple” in only first occurrence of document in collection as we used db.food.update.
If we use db.food.updateMany, then “apple” is replaced with “an apple” in its all occurrences

To update the document with "apple" at index 1 and push new value “cherry” in to the "fruits" array.
Before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
Process:
> db.food.update({"fruits.1":"apple"},{$push:{fruits:"cherry"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
after:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry" ] }
>

To update the document with "orange at 0th index and push new key value pairs in the "fruits" array.
Add new array price
Before:
> db.food.find()
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "an apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "jackfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #49

Process:
> db.food.update({"fruits.0":"orange"},{$push:{price:{orange:60,jackfruit:200,mango:120}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
After:
> db.food.find()
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "an apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "jackfruit", "mango" ], "price" : [ { "orange" :
60, "jackfruit" : 200, "mango" : 120 } ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
>
Here second document is updated with price

addToSet
To update the document with "apple" at index 1 and push(addToSet) new value “jamun”in to the
"fruits" array.
Before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry" ] }
Process:
> db.food.update({"fruits.1":"apple"},{$addToSet:{fruits:"jamun"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry", "jamun" ] }
>

this command > db.food.update({"fruits.1":"apple"},{$addToSet:{fruits:"jamun"}}


equivalent to > db.food.update({"fruits.1":"apple"},{$push:{fruits:"cherry"}})

pop
To update the document with "apple" at index 1 by popping an element from the list of elements present in
the array "fruits". The element popped is the one from the end of the array.
Before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry", "jamun" ] }
Task:
> db.food.update({"fruits.1":"apple"},{$pop:{fruits:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry" ] }
>
Here from the array “jamun” is popped up here fruits:1 indicate last element in list

To update the document with "apple" at index 1 by popping an element from the list of elements present in
the array "fruits". The element popped is the one from the beginning of the array.
Before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry" ] }
Task:
> db.food.update({"fruits.1":"apple"},{$pop:{fruits:-1}})
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #50

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


result:
> db.food.find({"fruits.1":"apple"})
>
Here in result we did not get any thing, as we removed or popup at beginning of array fruits, now the element at index 1
moved to index 0 , hence we did not get any out put as there is no match for "fruits.1":"apple"
Now we can obtain result by
> db.food.find({"fruits.0":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "apple", "cherry" ] }
>

pull

To update the documents having "banana" as an element in the array "fruits" and pop out the element
"banana" from those documents.
Before:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
Task:
> db.food.update({"fruits.0":"banana"},{$pull:{fruits:"banana"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
>

To update the document with "banana" by popping two elements from the list of elements present in the
array "fruits". The elements popped are "apple" and "cherry".
Before:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
Task:
> db.food.update({"fruits.0":"banana"},{$pullAll:{fruits:["apple","cherry"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
>

Here in the search found at first occurrence of criteria will be considered, not the other

Objective: To pull out an array element based on index position.


Note: There is no direct way of pulling the array elements by looking up their index numbers. However a
workaround is available.
Solution:

To pull out an array element based on index position.


First we set the element to null by index, then later delete element whose value is null by pull command
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #51

Before:
>db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
Step-1
Action: We set the element to null by index - "fruits.1":null
> db.food.updateMany({"fruits.0":"banana"},{$unset:{"fruits.1":null}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 1 }
intemediate result:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", null, "grapes" ] }
Step-2: delete element whose value is null by pull command
> db.food.updateMany({"fruits.0":"banana"},{$pull:{fruits:null}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 1 }
Result:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "grapes" ] }
>

Aggregate Function
What is MongoDB Aggregation?

In aggregation operation, MongoDB processes the data records and returns a single computed result. It actually
groups multiple documents and then performs aggregation operation on it and after that returns a single result to
the end user.

MongoDB can perform aggregation in 3 ways and they are as follows:

i. Aggregation Pipeline
ii. Map-Reduce Function
iii. Single Purpose Aggregation Method
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #52

i. MongoDB Aggregation Pipeline

Aggregation process in MongoDB is modeled on the concept of data processing pipelines. Multiple
documents enter the pipeline and then these documents are being transformed into aggregated results.
The operations being performed during pipeline include filter and document transformation in which they
operate like queries and modify the form of output document respectively.

The pipeline method provides efficient data aggregation using other operations present in MongoDB. It can also
operate on a sharded collection.

MongoDB Aggregation Pipeline Example


MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #53

ii. Map-Reduce

As the name Map-Reduce suggests it has two operations. Map operation, each document is being
processed along with emitting one or more objects for each document and reduce phase in which output of
map operation are being combined together.
MongoDB Map-reduce can optionally have a finalize stage in which it can do some final modifications in the
output of the document. Map-Reduce uses JavaScript to perform its operations including the finalize
operation.

MongoDB Mapreduce Example


MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #54

iii. Single Purpose Aggregation Operations


These operations aggregate all the documents from a single collection in MongoDB. Even though they provide
simple access to common aggregation operations they lack the flexibility and capability of map-reduce and
aggregation pipeline.

Single Purpose Aggregation Operation Example


Sample Data
Existing collections and documents in customer collection
> show collections
employee
food
student
> db.customer.insert([
... {custId:"C101",name:"Murali","accType":"S",balance:10200 },
... {custId:"C102",name:"Krishna","accType":"S",balance:15204 },
... {custId:"C103",name:"Soumya","accType":"S",balance:13092 },
... {custId:"C104",name:"Sailaja","accType":"S",balance:110400 },
... {custId:"C105",name:"Anamika Textiles","accType":"C",balance:1022200 },
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 5,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #55

"nRemoved" : 0,
"upserted" : [ ]
})
> show collections
customer
employee
food
student
> db.customer.find()
{ "_id" : ObjectId("5d745da59233b0160ec69416"), "custId" : "C101", "name" : "Murali", "accType" : "S", "balance" :
10200 }
{ "_id" : ObjectId("5d745da59233b0160ec69417"), "custId" : "C102", "name" : "Krishna", "accType" : "S", "balance" :
15204 }
{ "_id" : ObjectId("5d745da59233b0160ec69418"), "custId" : "C103", "name" : "Soumya", "accType" : "S", "balance" :
13092 }
{ "_id" : ObjectId("5d745da59233b0160ec69419"), "custId" : "C104", "name" : "Sailaja", "accType" : "S", "balance" :
110400 }
{ "_id" : ObjectId("5d745da59233b0160ec6941a"), "custId" : "C105", "name" : "Anamika Textiles", "accType" : "C",
"balance" : 1022200 }
>

Objective: Consider the collection "Customers" as given above. It has five documents.
Task: We would like to filter out those documents where the "AccType" has a value other than "S". After the
filter, We should be left with four documents where the "Acctype": "S". It is then required to group the
documents on the basis of CustID and sum up the "AccBal" for each unique "CustID". This is similar to the
output received with group by clause in RDBMS. Filter and display that group where the "TotAccBal" column
has a value greater than 15000.

Customers $match $group ( where balance > 15000 )


> db.customer.find().pretty() > db.customer.find({accType:"S"}).pretty()

{
"_id" : ObjectId("5d745da59233b0160ec69416"), {
"custId" : "C101", "_id":ObjectId("5d745da59233b0160ec69
"name" : "Murali", 416"),
"accType" : "S", "custId" : "C101",
"balance" : 10200 "name" : "Murali",
} "accType" : "S",
{ "balance" : 10200
"_id" : ObjectId("5d745da59233b0160ec69417"), }
"custId" : "C102", { {
"name" : "Krishna", "_id":ObjectId("5d745da59233b0160ec69 "_id" : ObjectId("5d745da59233b0160ec69417"),
"accType" : "S", 417"), "custId" : "C102",
"balance" : 15204 "custId" : "C102", "name" : "Krishna",
} "name" : "Krishna", "accType" : "S",
{ "accType" : "S", "balance" : 15204
"_id" : ObjectId("5d745da59233b0160ec69418"), "balance" : 15204 }
"custId" : "C103", } {
"name" : "Soumya", { "_id" : ObjectId("5d745da59233b0160ec69419"),
"accType" : "S", "_id":ObjectId("5d745da59233b0160ec69 "custId" : "C104",
"balance" : 13092 418"), "name" : "Sailaja",
} "custId" : "C103", "accType" : "S",
{ "name" : "Soumya", "balance" : 110400
"_id" : ObjectId("5d745da59233b0160ec69419"), "accType" : "S", }
"custId" : "C104", "balance" : 13092
"name" : "Sailaja", }
"accType" : "S", {
"balance" : 110400 "_id":ObjectId("5d745da59233b0160ec69
} 419"),
{ "custId" : "C104",
"_id" : ObjectId("5d745da59233b0160ec6941a"), "name" : "Sailaja",
"custId" : "C105", "accType" : "S",
"name" : "Anamika Textiles", "balance" : 110400
"accType" : "C", }
"balance" : 1022200
}
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #56

Stages of MongoDB Aggregation Pipeline

These are 7 stages of aggregation of Pipeline in MongoDB:

i. $project
ii. $match
iii. $group
iv. $sort
v. $skip & $limit
vi. $first & $last
vii. $unwind

i. $project :
This stage is used to select certain fields from a collection. We can also add, remove or reshape a key.
Syntax: { $project: { <specifications> } }
Specification Description
<field>: <1 or true> Specify the inclusion of a field.
_id: <0 or false> Specify the suppression of the _id field.
<field>: <expression> Add a new field or reset the value of an existing field.

List selected fields in documents from customer collection, By default ObjectId is displayed
> db.customer.aggregate( [ { $project : { custId:1,name:1 } } ] );
{ "_id" : ObjectId("5d745da59233b0160ec69416"), "custId" : "C101", "name" : "Murali" }
{ "_id" : ObjectId("5d745da59233b0160ec69417"), "custId" : "C102", "name" : "Krishna" }
{ "_id" : ObjectId("5d745da59233b0160ec69418"), "custId" : "C103", "name" : "Soumya" }
{ "_id" : ObjectId("5d745da59233b0160ec69419"), "custId" : "C104", "name" : "Sailaja" }
{ "_id" : ObjectId("5d745da59233b0160ec6941a"), "custId" : "C105", "name" : "Anamika Textiles" }
>

In this example ObjectId can be suppressed by _id:0

> db.customer.aggregate( [ { $project : { _id:0,custId:1,name:1 } } ] );


{ "custId" : "C101", "name" : "Murali" }
{ "custId" : "C102", "name" : "Krishna" }
{ "custId" : "C103", "name" : "Soumya" }
{ "custId" : "C104", "name" : "Sailaja" }
{ "custId" : "C105", "name" : "Anamika Textiles" }
>

Here we assign the labels such as AccountNo, Customername for the fields as we wish for our convenience

> db.customer.aggregate( [ { $project : { _id:0,AccountNo:"$custId",CustomerName:"$name" } } ] );


{ "AccountNo" : "C101", "CustomerName" : "Murali" }
{ "AccountNo" : "C102", "CustomerName" : "Krishna" }
{ "AccountNo" : "C103", "CustomerName" : "Soumya" }
{ "AccountNo" : "C104", "CustomerName" : "Sailaja" }
{ "AccountNo" : "C105", "CustomerName" : "Anamika Textiles" }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #57

Example –
db.example.aggregate([
{
$project:{
_id:1,
'dept':{$toUpper:'$name'},
'newexp':{$add:['$exp',10]}
}
}
])
Here we are creating ‘dept’ from earlier ‘name’ which was in upper case. And in ‘newexp’ we are adding the
experience as 10 years.

ii. $match
The MongoDB $match operator filters the documents to pass only those documents that match the specified
condition(s) to the next pipeline stage.

It is very much effective if you place the $match as early as possible in the aggregation pipeline that limits the
total number of documents in the aggregation pipeline.

When $match placed at the very beginning of a pipeline, the query can take advantage of indexes.

Note: The $where can not be used in $match queries as part of the aggregation pipeline

> db.customer.aggregate([{$match:{accType:"S"}}]);
{ "_id" : ObjectId("5d745da59233b0160ec69416"), "custId" : "C101", "name" : "Murali", "accType" : "S",
"balance" : 10200 }
{ "_id" : ObjectId("5d745da59233b0160ec69417"), "custId" : "C102", "name" : "Krishna", "accType" : "S",
"balance" : 15204 }
{ "_id" : ObjectId("5d745da59233b0160ec69418"), "custId" : "C103", "name" : "Soumya", "accType" : "S",
"balance" : 13092 }
{ "_id" : ObjectId("5d745da59233b0160ec69419"), "custId" : "C104", "name" : "Sailaja", "accType" : "S",
"balance" : 110400 }
>
In the above example we are aggregating documents that have a customer accType=”S”

Now we try to aggregate documents that have a customer accType=”C”

> db.customer.aggregate([{$match:{accType:"C"}}]);
{ "_id" : ObjectId("5d745da59233b0160ec6941a"), "custId" : "C105", "name" : "Anamika Textiles",
"accType" : "C", "balance" : 1022200 }
>

Now we try to aggregate documents that have balance >15000


> db.customer.aggregate([{$match:{balance:{$gt:15000}}}]);
{ "_id" : ObjectId("5d745da59233b0160ec69417"), "custId" : "C102", "name" : "Krishna", "accType" : "S",
"balance" : 15204 }
{ "_id" : ObjectId("5d745da59233b0160ec69419"), "custId" : "C104", "name" : "Sailaja", "accType" : "S",
"balance" : 110400 }
{ "_id" : ObjectId("5d745da59233b0160ec6941a"), "custId" : "C105", "name" : "Anamika Textiles",
"accType" : "C", "balance" : 1022200 }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #58

Advanced example
Count the number of customers who have balance > 15000
> db.customer.aggregate([{$match:{balance:{$gt:15000}}},{$group:{_id:0,count:{$sum:1}}}]);
{ "_id" : 0, "count" : 3 }
>

Example –
db.example.aggregate([
{
$match:{
name:'xyz'
}
}
])
Here we are aggregating documents that have a name equal to xyz.

iii. $group
It does the work as the name says. It groups all documents based on some keys.

> db.customer.aggregate([{$group:{_id:null,count:{$sum:1}}}]);
{ "_id" : null, "count" : 5 }
>
In this example we count the number of documents

No we try to count the customers whose balance >15000 /Total number of customers / acccount Type is “S”
db.customer.aggregate([{$match:{balance:{$gt:15000}}},{$group:{_id:null,count:{$sum:1}}}]);
{ "_id" : null, "count" : 3 }
> db.customer.aggregate([{$group:{_id:null,count:{$sum:1}}}]);
{ "_id" : null, "count" : 5 }
> db.customer.aggregate([{$match:{accType:"S"}},{$group:{_id:null,count:{$sum:1}}}]);
{ "_id" : null, "count" : 4 }

Task-1 - Total balance in each account type


> db.customer.aggregate([{$group:{_id:"$accType",TotBalance:{$sum:"$balance"}}}]);
{ "_id" : "C", "TotBalance" : 1022200 }
{ "_id" : "S", "TotBalance" : 148896 }
>
Task-2 - Total balance in account type = “S”
> db.customer.aggregate([{$match:{accType:"S"}},{$group:{_id:"$accType",TotBalance:
{$sum:"$balance"}}}]);
{ "_id" : "S", "TotBalance" : 148896 }

in the above example total balance in the account Type “S” - savings accont
in the above example _id:”$accType” is the vlaue to be displayed
Task-3 - Total balance in account type = “C”
> db.customer.aggregate([{$match:{accType:"C"}},{$group:{_id:"$accType",TotBalance:
{$sum:"$balance"}}}]);
{ "_id" : "C", "TotBalance" : 1022200 }
>
in the above example total balance in the account Type “C” - current account
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #59

Task-1 Average balance


> db.customer.aggregate([{$group:{_id:"$accType",TotBalance:{$avg:"$balance"}}}]);
{ "_id" : "C", "TotBalance" : 1022200 }
{ "_id" : "S", "TotBalance" : 37224 }

Task-2 Maximum balance


> db.customer.aggregate([{$group:{_id:"$accType",MaxBalance:{$max:"$balance"}}}]);
{ "_id" : "C", "MaxBalance" : 1022200 }
{ "_id" : "S", "MaxBalance" : 110400 }
Task-3 Minimum balance
> db.customer.aggregate([{$group:{_id:"$accType",MinBalance:{$min:"$balance"}}}]);
{ "_id" : "C", "MinBalance" : 1022200 }
{ "_id" : "S", "MinBalance" : 10200 }
>
Task-4 First document- Customer balance by account type
> db.customer.aggregate([{$group:{_id:"$accType",Balance:{$first:"$balance"}}}]);
{ "_id" : "C", "Balance" : 1022200 }
{ "_id" : "S", "Balance" : 10200 }
Task-5 Last document – Customer balance by account type
> db.customer.aggregate([{$group:{_id:"$accType",Balance:{$last:"$balance"}}}]);
{ "_id" : "C", "Balance" : 1022200 }
{ "_id" : "S", "Balance" : 110400 }
>

Example –

db.example.aggregate([
{
$group:{
_id:{'dept':'$name'},
employee_count:{$sum:10}
}
}
])

iv. $sort

It is used to sort all the documents.

Example –
Here we want to group all department in ascending order and then find the count of employees.

Task-1
> db.customer.aggregate([{$group:{_id:"$accType",TotBalance:{$sum:"$balance"}}},{$sort:{_id:1}}]);
{ "_id" : "C", "TotBalance" : 1022200 }
{ "_id" : "S", "TotBalance" : 148896 }
Task-2
> db.customer.aggregate([{$group:{_id:"$accType",TotBalance:{$sum:"$balance"}}},{$sort:{_id:-1}}]);
{ "_id" : "S", "TotBalance" : 148896 }
{ "_id" : "C", "TotBalance" : 1022200 }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #60

db.example.aggregate([
{
$group:{
_id:'$name',
employee_count:{$sum:1}
}
},
{
$sort:{
_id:1
}
}
])

v. $skip & $limit

Using skip we can skip forward in the list of all documents for the given limit.
Using limit we can limit the number of documents we want to look at by specifying the limit as per our
convenience.
Example –
db.example.aggregate([
{
$group:{
_id:'$name',
employee_count:{$sum:10}
}
},
{
$sort:{
_id:1
}
},
{
$skip:4
},
{
$limit:5
}
])

vi. $first & $last

It is used to get the first and last values in each group of documents.
Example –

db.example.aggregate([
{
$group:{
_id:'$name',
employee_count:{$sum:1},
record:{ $first:'$code'}
}
}
])
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #61

vii. $unwind

We can use unwind for all documents, that are using arrays.

Example –

Let’s say that we are having a sample document:

{
a:abcdata,
b:xyzdata,
c:[a1,a2,a3]
}

Also, see – MongoDB ObjectID and Atomic Operations

After this if we perform unwind operation on c then we will get 3 documents as follows:

{
a:abcdata,
b:xyzdata,
c:a1
}
{
a:abcdata,
b:xyzdata,
c:a2
}
{
a:abcdata,
b:xyzdata,
c:a3
}

****

MapReduce with MongoDB


MongoDB provides us with a MapReduce command, and in the following diagram we can observe the life
cycle of the MapReduce process in MongoDB. We start with a Collection or a Query;
Each document in the collection will call the map function. Then, with the emit function, we will create
an intermediate hash-map (see the following diagram) with a list of pairs (key-value).
Next, the reduce function will iterate the intermediate hash-map and will apply some operations to all
values of each key. Finally, the process will create a brand new collection with the output.
The map/reduce functions in MongoDB will be programmed with JavaScript:
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #62

Map function
The map function will call the emit function one or more times (above diagram). We can access all the
attributes of each document in the collection with the this keyword.
The intermediate hash-map contains only unique keys, so if the emit function sends a key that is already in
the hash-map, the value is going to be inserted in a list of values.
Each record in the hash-map will look like this: key:One, value:[1,2,3,...] .
Here, we can see a sample code of the map function:

function(){
emit(this._id, {count: 1});
}

Reduce function
The reduce function will receive two arguments: the key and values (one or a list of values). This function is
called for each record in the hash-map.
In the following code, we can see a sample reduce function. In this case, the function returns the total count for
each key:
function(key, values) {
total = 0;
for (var i = 0; i < values.length; ++i) {
total += values[i].count;
};
return {count: total};
}
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #63

The above map-reduce function will query the collection, and then map
db.collection.mapReduce the output documents to the emit key – value pairs. After this, it is reduced
( based on the keys that have multiple values.
function() {emit(key, value);}, Map: It is a JavaScript function. It is used to map a value with a key and
//Define map function produces a key-value pair.

function(key,values) Reduce: It is a JavaScript function. It is used to reduce or group together


{return reduce Function all the documents which have the same key.
},
{ Out: It is used to specify the location of the map-reduce query output
//Define reduce function
out: collection, Query:It is used to specify the optional selection criteria for selecting
query: document, documents
sort: document, Sort: It is used to specify the optional sort criteria
limit: number Limit: It is used to specify the optional maximum number of documents
} which are desired to be returned.
)

Collection :author has 6 documents


db.author.find().pretty()
{
"_id" : ObjectId("5d75134647bf7030afa9a26b"),
"book_title" : "MongoDB Tutorial",
"author_name" : "aparajita",
"status" : "active",
"publish_year" : "2016"
}
{
"_id" : ObjectId("5d75139f47bf7030afa9a26c"),
"book_title" : "Software Testing",
"author_name" : "aparajita",
"status" : "active",
"publish_year" : "2015"
}
{
"_id" : ObjectId("5d7513d247bf7030afa9a26d"),
"book_title" : "Node.Js Tutorial",
"author_name" : "keethika",
"status" : "active",
"publish_year" : "2016"
}
{
"_id" : ObjectId("5d75141f47bf7030afa9a26e"),
"book_title" : "PHP7 Tutorial",
"author_name" : "aparajita",
"status" : "passive",
"publish_year" : "2016"
}
{
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #64

"_id" : ObjectId("5d75144647bf7030afa9a26f"),
"book_title" : "Parallel Computing",
"author_name" : "rajaraman",
"status" : "active",
"publish_year" : "2014"
}
{
"_id" : ObjectId("5d75147b47bf7030afa9a270"),
"book_title" : "Fundamentals of Computers",
"author_name" : "rajaraman",
"status" : "passive",
"publish_year" : "2010"
}
>

Example1 MapReduce function

Now, we are going to use the mapReduce function on our author collection in order to select all the active
books, group them together on the basis of author_name and then count the number of books by each
author by using the following code in MongoDB.

> db.author.mapReduce( The result shows above is a total of 4 documents


... function() { emit(this.author_name,1)}, which are matched by the query (status:”active”),
... function(key, values) {return Array.sum(values)}, the map function has produced 4 documents with key-
... {query:{status:"active"}, value pairs and
... out:"author_total"});
{ "result" : "author_total", "ok" : 1 } finally the reduce function has grouped all the mapped
documents which have the same keys into 1.
>
output :
>db.author_total.find();
{ "_id" : "aparajita", "value" : 2 }
{ "_id" : "rajaraman", "value" : 1 }
{ "_id" : "keethika", "value" : 1 }
>
>

Code with find () with active authors Explanation


> db.author.mapReduce( function()
{ emit(this.author_name,1)}, function(key, values) We can use the find operator in order to see the result
{return Array.sum(values)}, {query: of this mapReduce query by writing the following
{status:"active"},out:"author_total"}); lines of code

{ "result" : "author_total", "ok" : 1 }

Output:
> db.author_total.find();
{ "_id" : "aparajita", "value" : 2 }
{ "_id" : "rajaraman", "value" : 1 }
{ "_id" : "keethika", "value" : 1 }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #65

Code with find () with “passive” authors Explanation


> db.author.mapReduce(
function() { emit(this.author_name,1)},
function(key, values) {return Array.sum(values)},
... {query:{status:"passive"},
... out:"author_total"});
{ "result" : "author_total", "ok" : 1 }

> db.author_total.find();
{ "_id" : "rajaraman", "value" : 1 }
{ "_id" : "aparajita", "value" : 1 }
>

6.5.11 Java Script Programming


Objective: To compute the factorial of a given positive number.
The user is required to create a function by the name "factorial" and insert it into the "system.js" collection.
Before we proceed, a quick check on what is contained in the "system.js" collection:

db.system.js.find();
As per the screenshot above, currently there are no functions in the system.js collection.

Act:
db.system.js.insert({_id:"factorial",
value:function (n) {
if (n==1) return 1; else
return n * factorial(n-1);
}
});

Confirm the presence of the "factorial" function in the system.js collection.


To execute the function factorial use eval() method
for e.g
>db.eval(“factorial(3)”);
6
>
>db.eval(“factorial(5)”);
125
>

6.5.12 Cursors in MongoDB


Objective: To create a collection by the name "alphabets" and insert documents in it containing two fields,
"_id" and "alphabet". The values stored in the "alphabet" field should be "a", "b", "c", "d", etc. with one value
stored per document. There should be 26 documents in all. We need to use cursor to iterate through the
"alphabets" collection.

'Note: "Alphabets" is the name of the collection and "alphabet" is the name of the field.(key)
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #66

Act: To create the collection "alphabets" with its 26 documents


> db.alphabets.insert({_id: 1,alphabet: "a"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 2,alphabet: "b"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 3,alphabet: "c"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 4,alphabet: "d"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 5,alphabet: "e"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 6,alphabet: "f"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 7,alphabet: "g"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 8,alphabet: "h"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 9,alphabet: "i"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 10,alphabet: "j"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 11,alphabet: "k"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 12,alphabet: "l"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 13,alphabet: "m"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 14,alphabet: "n"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 15,alphabet: "o"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 16,alphabet: "p"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 17,alphabet: "q"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 18,alphabet: "r"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 19,alphabet: "s"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 20,alphabet: "t"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 21,alphabet: "u"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 22,alphabet: "v"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 23,alphabet: "w"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 24,alphabet: "x"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 25,alphabet: "y"});
WriteResult({ "nInserted" : 1 })
> db.alphabets.insert({_id: 26,alphabet: "z"});
WriteResult({ "nInserted" : 1 })
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #67

>
db.alphabets.find();
{ "_id" : 1, "alphabet" : "a" }
{ "_id" : 2, "alphabet" : "b" }
{ "_id" : 3, "alphabet" : "c" }
{ "_id" : 4, "alphabet" : "d" }
{ "_id" : 5, "alphabet" : "e" }
{ "_id" : 6, "alphabet" : "f" }
{ "_id" : 7, "alphabet" : "g" }
{ "_id" : 8, "alphabet" : "h" }
{ "_id" : 9, "alphabet" : "i" }
{ "_id" : 10, "alphabet" : "j" }
{ "_id" : 11, "alphabet" : "k" }
{ "_id" : 12, "alphabet" : "l" }
{ "_id" : 13, "alphabet" : "m" }
{ "_id" : 14, "alphabet" : "n" }
{ "_id" : 15, "alphabet" : "o" }
{ "_id" : 16, "alphabet" : "p" }
{ "_id" : 17, "alphabet" : "q" }
{ "_id" : 18, "alphabet" : "r" }
{ "_id" : 19, "alphabet" : "s" }
{ "_id" : 20, "alphabet" : "t" }
Type "it" for more
>

The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor.
However, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the
cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results.
The following examples describe ways to manually iterate the cursor to access the documents or to use the
iterator index.
Manually Iterate the Cursor
In the mongo shell, when you assign the cursor returned from the find() method to a variable using the var
keyword, the cursor does not automatically iterate.
You can call the cursor variable in the shell to iterate up to 20 times and print the matching documents, as in the
following example:

> var myCursor = db.alphabets.find();


> myCursor;
{ "_id" : 1, "alphabet" : "a" }
{ "_id" : 2, "alphabet" : "b" }
{ "_id" : 3, "alphabet" : "c" }
{ "_id" : 4, "alphabet" : "d" }
{ "_id" : 5, "alphabet" : "e" }
{ "_id" : 6, "alphabet" : "f" }
{ "_id" : 7, "alphabet" : "g" }
{ "_id" : 8, "alphabet" : "h" }
{ "_id" : 9, "alphabet" : "i" }
{ "_id" : 10, "alphabet" : "j" }
{ "_id" : 11, "alphabet" : "k" }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #68

{ "_id" : 12, "alphabet" : "l" }


{ "_id" : 13, "alphabet" : "m" }
{ "_id" : 14, "alphabet" : "n" }
{ "_id" : 15, "alphabet" : "o" }
{ "_id" : 16, "alphabet" : "p" }
{ "_id" : 17, "alphabet" : "q" }
{ "_id" : 18, "alphabet" : "r" }
{ "_id" : 19, "alphabet" : "s" }
{ "_id" : 20, "alphabet" : "t" }
Type "it" for more
>

Let us now look at designing manual cursors to iterate through me documents in the "alphabets" collection. We
will use two methods with manual cursors:
1. hasNext()
2. next().
Method 1: hasNext() method. Return value: Boolean. The hasNext() method returns true if the cursor
returned by the db.Collection.find() query can iterate further to return more documents.

Method 2: next() method. The next() method returns the next document in the cursor as returned by the
db.collection.find() method.

print(tojson()) method with hasNext() printjson() helper method to replace print(tojson())


withhasNext()
var myCur = db.alphabets.find({}); > var myCur = db.alphabets.find({});
> >
> while (myCur.hasNext()) { > while(myCur.hasNext()) { printjson(myCur.next()); }
... print(tojson(myCur.next())); } { "_id" : 1, "alphabet" : "a" }
{ "_id" : 1, "alphabet" : "a" } { "_id" : 2, "alphabet" : "b" }
{ "_id" : 2, "alphabet" : "b" } { "_id" : 3, "alphabet" : "c" }
{ "_id" : 3, "alphabet" : "c" } { "_id" : 4, "alphabet" : "d" }
{ "_id" : 4, "alphabet" : "d" } { "_id" : 5, "alphabet" : "e" }
{ "_id" : 5, "alphabet" : "e" } { "_id" : 6, "alphabet" : "f" }
{ "_id" : 6, "alphabet" : "f" } { "_id" : 7, "alphabet" : "g" }
{ "_id" : 7, "alphabet" : "g" } { "_id" : 8, "alphabet" : "h" }
{ "_id" : 8, "alphabet" : "h" } { "_id" : 9, "alphabet" : "i" }
{ "_id" : 9, "alphabet" : "i" } { "_id" : 10, "alphabet" : "j" }
{ "_id" : 10, "alphabet" : "j" } { "_id" : 11, "alphabet" : "k" }
{ "_id" : 11, "alphabet" : "k" } { "_id" : 12, "alphabet" : "l" }
{ "_id" : 12, "alphabet" : "l" } { "_id" : 13, "alphabet" : "m" }
{ "_id" : 13, "alphabet" : "m" } { "_id" : 14, "alphabet" : "n" }
{ "_id" : 14, "alphabet" : "n" } { "_id" : 15, "alphabet" : "o" }
{ "_id" : 15, "alphabet" : "o" } { "_id" : 16, "alphabet" : "p" }
{ "_id" : 16, "alphabet" : "p" } { "_id" : 17, "alphabet" : "q" }
{ "_id" : 17, "alphabet" : "q" } { "_id" : 18, "alphabet" : "r" }
{ "_id" : 18, "alphabet" : "r" } { "_id" : 19, "alphabet" : "s" }
{ "_id" : 19, "alphabet" : "s" } { "_id" : 20, "alphabet" : "t" }
{ "_id" : 20, "alphabet" : "t" } { "_id" : 21, "alphabet" : "u" }
{ "_id" : 21, "alphabet" : "u" } { "_id" : 22, "alphabet" : "v" }
{ "_id" : 22, "alphabet" : "v" } { "_id" : 23, "alphabet" : "w" }
{ "_id" : 23, "alphabet" : "w" } { "_id" : 24, "alphabet" : "x" }
{ "_id" : 24, "alphabet" : "x" } { "_id" : 25, "alphabet" : "y" }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #69

{ "_id" : 25, "alphabet" : "y" } { "_id" : 26, "alphabet" : "z" }


{ "_id" : 26, "alphabet" : "z" } >
>

var myCur = db.alphabets.find({});


>
> myCur.forEach(printjson);
{ "_id" : 1, "alphabet" : "a" }
{ "_id" : 2, "alphabet" : "b" }
{ "_id" : 3, "alphabet" : "c" }
{ "_id" : 4, "alphabet" : "d" }
{ "_id" : 5, "alphabet" : "e" }
{ "_id" : 6, "alphabet" : "f" }
{ "_id" : 7, "alphabet" : "g" }
{ "_id" : 8, "alphabet" : "h" }
{ "_id" : 9, "alphabet" : "i" }
{ "_id" : 10, "alphabet" : "j" }
{ "_id" : 11, "alphabet" : "k" }
{ "_id" : 12, "alphabet" : "l" }
{ "_id" : 13, "alphabet" : "m" }
{ "_id" : 14, "alphabet" : "n" }
{ "_id" : 15, "alphabet" : "o" }
{ "_id" : 16, "alphabet" : "p" }
{ "_id" : 17, "alphabet" : "q" }
{ "_id" : 18, "alphabet" : "r" }
{ "_id" : 19, "alphabet" : "s" }
{ "_id" : 20, "alphabet" : "t" }
{ "_id" : 21, "alphabet" : "u" }
{ "_id" : 22, "alphabet" : "v" }
{ "_id" : 23, "alphabet" : "w" }
{ "_id" : 24, "alphabet" : "x" }
{ "_id" : 25, "alphabet" : "y" }
{ "_id" : 26, "alphabet" : "z" }
>
Indexes
Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform
a collection scan, i.e. scan every document in a collection, to select those documents that match the query
statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of
documents it must inspect.
Indexes are special data structures that store a small portion of the collection’s 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.
The ordering of the index entries supports efficient equality matches and range-based query operations. In
addition, MongoDB can return sorted results by using the ordering in the index.
The following diagram illustrates a query that selects and orders the matching documents using an index:
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #70

Fundamentally, indexes in MongoDB are similar to indexes in other database systems.


MongoDB defines indexes at the collection level and supports indexes on any field or sub-field of the
documents in a MongoDB collection.

Default _id Index

MongoDB creates a unique index on the _id field during the creation of a collection.
The _id index prevents clients from inserting two documents with the same value for the _id field. You
cannot drop this index on the _id field.

Create an Index
To create an index in the Mongo Shell, use db.collection.createIndex().
db.collection.createIndex( <key and index type specification>, <options> )
The following example creates a single key descending index on the name field:
db.collection.createIndex({ name: -1 })
The db.collection.createIndex method only creates an index if an index of the same specification does not
already exist.
Note: MongoDB indexes use a B-tree data structure.
Index Names

The default name for an index is the concatenation of the indexed keys and each key’s direction in the
index ( i.e. 1 or -1) using underscores as a separator.
For example, an index created on { item : 1, quantity: -1 } has the name item_1_quantity_-1.

You can create indexes with a custom name, such as one that is more human-readable than the default.
For example, consider an application that frequently queries the products collection to populate data on existing
inventory.
The following createIndex() method creates an index on item and quantity named query for inventory:
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #71

db.products.createIndex(
{ item: 1, quantity: -1 } ,
{ name: "query for inventory" }
)

You can view index names using the db.collection.getIndexes() method.


You cannot rename an index once created. Instead, you must drop and re-create the index with a new
name.

Index Types
MongoDB provides a number of different index types to support specific types of data and queries.
a. Single Field
b. Compound Index
c. Multikey Index
d. Geospatial Index
e. Text Indexes
f. Hashed Indexes

a. Single Field :
In addition to the MongoDB-defined _id index, MongoDB supports the creation of user-defined
ascending/descending indexes on a single field of a document.

For a single-field index and sort operations, the sort order (i.e. ascending or descending) of the index key
does not matter because MongoDB can traverse the index in either direction.

b. Compound Index
MongoDB also supports user-defined indexes on multiple fields, i.e. compound indexes.
The order of fields listed in a compound index has significance.
For instance, if a compound index consists of { userid: 1, score: -1 }, the index sorts first by userid and then,
within each userid value, sorts by score.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #72

For compound indexes and sort operations, the sort order (i.e. ascending or descending) of the index keys can
determine whether the index can support a sort operation.

c. Multikey Index

MongoDB uses multikey indexes to index the content stored in arrays. If you index a field that holds an
array value, MongoDB creates separate index entries for every element of the array. These multikey
indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays.
MongoDB automatically determines whether to create a multikey index if the indexed field contains an
array value; you do not need to explicitly specify the multikey type.

d. Geo-spatial Index

To support efficient queries of geospatial coordinate data, MongoDB provides two special indexes:
(i) 2d indexes that uses planar geometry when returning results
(ii) 2d sphere indexes that use spherical geometry to return results.

e. Text Indexes

MongoDB provides a text index type that supports searching for string content in a collection. These text
indexes do not store language-specific stop words (e.g. “the”, “a”, “or”) and stem the words in a collection
to only store root words.

f. Hashed Indexes

To support hash based sharding, MongoDB provides a hashed index type, which indexes the hash of the
value of a field. These indexes have a more random distribution of values along their range, but only support
equality matches and cannot support range-based queries.

6.5.14 MongoImport

MongoImport used at the command prompt imports CSV (Comma Separated Values) or TSV (Tab Separated
Values) files or JSON (Java Script Object Notation) documents into MongoDB.
(in Ubuntu - $ prompt / Windows - command prompt)
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #73

To import data, open a new Terminal/Command Prompt window and enter mongoimport followed by
parameters such as database name, collection name, source file name, etc.

Import JSON File

Here's an example of running mongoimport to import a JSON file.

You might remember that we previously used mongoexport to export the artists collection to a JSON file.

$ mongoimport --db mydb --file /home/hduser/17207/artists.json

Here mydb – database in mongo db where we wish to import data


/home/hduser/17207/artists.json – here this is the location of the file from which we need to import
Resulting message:

2016-07-12T13:34:04.904+0700 no collection specified


2016-07-12T13:34:04.905+0700 using filename 'artists' as collection
2016-07-12T13:34:04.911+0700 connected to: localhost
2016-07-12T13:34:04.968+0700 imported 13 documents

Now, let's switch back to our mongo Terminal/Command Prompt window and retrieve the list of collections in
our database:

>show collections

Result:

artists
musicians
producers

Now we'll query our revived collection.

db.artists.find()

Result:

{ "_id" : 1, "artistname" : "AC/DC" }


{ "_id" : ObjectId("5781d7f248ef8c6b3ffb014d"), "artistname" : "The Kooks" }
{ "_id" : ObjectId("5780fbf948ef8c6b3ffb0149"), "artistname" : "The Tea Party" }
{ "_id" : ObjectId("5781d7f248ef8c6b3ffb014f"), "artistname" : "Gang of Four" }
{ "_id" : ObjectId("5781d7f248ef8c6b3ffb014e"), "artistname" : "Bastille" }
{ "_id" : ObjectId("5781c9ac48ef8c6b3ffb014a"), "artistname" : "Jorn Lande" }
{ "_id" : ObjectId("5781f85d48ef8c6b3ffb0150"), "artistname" : "Deep Purple", "albums" : [ { "album" :
"Machine Head", "year" : 1972, "genre" : "Rock" }, { "album" : "Stormbringer", "year" : 1974, "genre" :
"Rock" } ] }
{ "_id" : ObjectId("578214f048ef8c6b3ffb0159"), "artistname" : "Miles Davis", "albums" : [ { "album" : "Kind
of Blue", "year" : 1959, "genre" : "Jazz" }, { "album" : "Bitches Brew", "year" : 1970, "genre" : "Jazz" } ] }
{ "_id" : ObjectId("578217c248ef8c6b3ffb015a"), "artistname" : "Robben Ford", "albums" : [ { "album" :
"Bringing it Back Home", "year" : 2013, "genre" : "Blues" }, { "album" : "Talk to Your Daughter", "year" :
1988, "genre" : "Blues" } ] }
{ "_id" : 2, "artistname" : "Prince", "address" : { "street" : "Audubon Road", "city" : "Chanhassen", "state" :
"Minnesota", "country" : "United States" } }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #74

{ "_id" : 4, "artistname" : "Rush" }


{ "_id" : 3, "artistname" : "Moby", "albums" : [ { "album" : "Play", "year" : 1999, "genre" : "Electronica" },
{ "album" : "Long Ambients 1: Calm. Sleep.", "year" : 2016, "genre" : "Ambient" } ] }
{ "_id" : ObjectId("578217c248ef8c6b3ffb015b"), "artistname" : "Snoop Dogg", "albums" : [ { "album" : "Tha
Doggfather", "year" : 1996, "genre" : "Rap" }, { "album" : "Reincarnated", "year" : 2013, "genre" :
"Reggae" } ] }

Specify a Collection Name with JSON file

You can use the --collection argument to provide a name of the collection that the data should go into.

Let's import another file, but this time, specify a collection name:

$ mongoimport --db mydb --collection jazz --file /home/hduser/17207/miles_davis.json

Here mydb – database in mongo db where we wish to import data


jazz – collection name of mydb in to which we need to import json file
/home/hduser/17207/miles_davis.json – here this is the location of the file from which we need to import

Resulting message:

2016-07-12T14:09:01.793+0700 connected to: localhost


2016-07-12T14:09:01.849+0700 imported 1 document

Now switch back to mongoDB and check the list of collections:

>show collections

Resulting message:
artists
jazz
musicians
producers
And finally, query the jazz collection:

>db.jazz.find().pretty()

Resulting message:

{
"_id" : ObjectId("578214f048ef8c6b3ffb0159"),
"artistname" : "Miles Davis",
"albums" : [
{
"album" : "Kind of Blue",
"year" : 1959,
"genre" : "Jazz"
},
{
"album" : "Bitches Brew",
"year" : 1970,
"genre" : "Jazz"
}
]
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #75

Import CSV File

You can import a CSV file by using --type csv

if the CSV file has a header row, use --headerline to tell mongoimport to use the first line to determine the
name of the fields in the resulting document.

If the CSV file doesn't have a header row, use the --fields parameter to set the field names.

Case:1 -With Header Row


Here's an example of importing a document with a header row.

The contents of the CSV file:

_id,albumname,artistname
1,Killers,"Iron Maiden"
2,Powerslave,"Iron Maiden"
12,"Somewhere in Time","Iron Maiden"
3,"Surfing with the Alien","Joe Satriani"
10,"Flying in a Blue Dream","Joe Satriani"
11,"Black Swans and Wormhole Wizards","Joe Satriani"
6,"Out of the Loop","Mr Percival"
7,"Suck on This",Primus
8,"Pork Soda",Primus
9,"Sailing the Seas of Cheese",Primus

Import the file:


$ mongoimport --db mydb --collection catalog --type csv --headerline --file /home/hduser/17207/catalog.csv

Here mydb – database in mongo db where we wish to import data


catalog – collection name of mydb in to which we need to import json file
/home/hduser/17207/catalog.csv – here this is the location of the file from which we need to import
Query the collection:
--headerline - csv file has header line

> db.catalog.find()
{ "_id" : 2, "albumname" : "Powerslave", "artistname" : "Iron Maiden" }
{ "_id" : 1, "albumname" : "Killers", "artistname" : "Iron Maiden" }
{ "_id" : 3, "albumname" : "Surfing with the Alien", "artistname" : "Joe Satriani" }
{ "_id" : 12, "albumname" : "Somewhere in Time", "artistname" : "Iron Maiden" }
{ "_id" : 10, "albumname" : "Flying in a Blue Dream", "artistname" : "Joe Satriani" }
{ "_id" : 6, "albumname" : "Out of the Loop", "artistname" : "Mr Percival" }
{ "_id" : 7, "albumname" : "Suck on This", "artistname" : "Primus" }
{ "_id" : 8, "albumname" : "Pork Soda", "artistname" : "Primus" }
{ "_id" : 11, "albumname" : "Black Swans and Wormhole Wizards", "artistname" : "Joe Satriani" }
{ "_id" : 9, "albumname" : "Sailing the Seas of Cheese", "artistname" : "Primus" }

Case:2 - Without Header Row

Here's another CSV file, but this one doesn't have a header row:
Mutt Lange, 1948
John Petrucci, 1967
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #76

DJ Shadow, 1972
George Clinton, 1941

Now we'll import it and specify the field names to use:

> mongoimport --db mydb --collection producers --type csv --fields name,born –file
/home/hduser/17207/producers.csv

Here we are specifying the fields name,born to the collection during import
Query the collection:

> db.producers.find()
{ "_id" : 1, "name" : "Bob Rock" }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b72"), "name" : "John Petrucci", "born" : 1967 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b73"), "name" : "Mutt Lange", "born" : 1948 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b74"), "name" : "George Clinton", "born" : 1941 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b75"), "name" : "DJ Shadow", "born" : 1972 }

You'll see that the ObjectId field has been automatically created and populated for us.

Also, we already had one document in this collection before we ran the import: {"_id" : 1,"name":"Bob Rock"}.
Therefore, you can see that the import has simply added to the collection (as opposed to replacing it and all its
contents).

6.5.15 MongoExport

MongoExport used at the command prompt exports MongoDB JSON documents into CSV (Comma
Separated Values) or TSV (Tab Separated Values) files or JSON (Java Script Object Notation) documents.
In MongoDB, you can export data using the mongoexport utility.

You can use the mongoexport utility to export data from your MongoDB database, to a JSON or CSV file.

The utility is located in the MongoDB bin directory (eg, /mongodb/bin). When you run the utility, provide
the name of the database, the collection, and the file you want it to be exported to.

To export data, first open a new Terminal/Command Prompt window, then type the applicable command.

Export a Collection to a JSON File

Here, we use mongoexport to export the artists collection to a JSON file:

$mongoexport --db mydb --collection artists --out //home/hduser/17207/artists.json

→ Here mydb – database in mongo db from which we wish to export data


→ artists – collection name of mydb from which we need to export file
→ /home/hduser/17207/artists.json here this is the location of file to which we need to export

in the above command artists is the collection in mongodb database


Resulting message:

2016-07-12T09:57:37.613+0700 connected to: localhost


2016-07-12T09:57:37.614+0700 exported 13 records
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #77

Resulting file:

{"_id":{"$oid":"5780fbf948ef8c6b3ffb0149"},"artistname":"The Tea Party"}


{"_id":{"$oid":"5781c9ac48ef8c6b3ffb014a"},"artistname":"Jorn Lande"}
{"_id":1.0,"artistname":"AC/DC"}
{"_id":{"$oid":"5781d7f248ef8c6b3ffb014d"},"artistname":"The Kooks"}
{"_id":{"$oid":"5781d7f248ef8c6b3ffb014e"},"artistname":"Bastille"}
{"_id":{"$oid":"5781d7f248ef8c6b3ffb014f"},"artistname":"Gang of Four"}
{"_id":{"$oid":"5781f85d48ef8c6b3ffb0150"},"artistname":"Deep Purple","albums":[{"album":"Machine
Head","year":1972.0,"genre":"Rock"},{"album":"Stormbringer","year":1974.0,"genre":"Rock"}]}
{"_id":{"$oid":"578214f048ef8c6b3ffb0159"},"artistname":"Miles Davis","albums":[{"album":"Kind of
Blue","year":1959.0,"genre":"Jazz"},{"album":"Bitches Brew","year":1970.0,"genre":"Jazz"}]}
{"_id":{"$oid":"578217c248ef8c6b3ffb015a"},"artistname":"Robben Ford","albums":[{"album":"Bringing it
Back Home","year":2013.0,"genre":"Blues"},{"album":"Talk to Your
Daughter","year":1988.0,"genre":"Blues"}]}
{"_id":{"$oid":"578217c248ef8c6b3ffb015b"},"artistname":"Snoop Dogg","albums":[{"album":"Tha
Doggfather","year":1996.0,"genre":"Rap"},{"album":"Reincarnated","year":2013.0,"genre":"Reggae"}]}
{"_id":2.0,"artistname":"Prince","address":{"street":"Audubon
Road","city":"Chanhassen","state":"Minnesota","country":"United States"}}
{"_id":3.0,"artistname":"Moby","albums":[{"album":"Play","year":1999.0,"genre":"Electronica"},
{"album":"Long Ambients 1: Calm. Sleep.","year":2016.0,"genre":"Ambient"}]}
{"_id":4.0,"artistname":"Rush"}

If you find that you can't run mongoexport, be sure that you've either exited the mongo utility,
or
opened a new Terminal/Command Prompt window before running mongoexport, as it is a separate utility.

The above command assumes that the MongoDB bin directory is in your PATH. If it's not, you will need to use
the full path to the mongoexport file.
For example, /mongodb/bin/mongoexport or wherever your MongoDB deployment is installed.

If you don't provide a file path for the exported file, it will be created wherever you are located when you run
the command. Either provide the full path, or navigate to where you'd like the data file to be written before you
run the command.

Export a Collection to a CSV File

To export to a CSV file, add --type=csv to the command.

You must also specify the fields in the MongoDB documents to export.
Here, we use mongoexport to export the artists collection to a CSV file. We export the _id and artistname fields.
We've also given the file name a .csv extension.

$mongoexport --db mydb --collection artists --type=csv --fields _id,artistname --out /home/hduser/17207/artists.csv

Here mydb – database in mongo db where we wish to import data


artists – collection name of mydb from which we need to export file
/home/hduser/17207/artists.csv here this is the location & file to which we need to export

in the above command artists is the collection in mongodb database


/home/hduser/17207/artists.csv – is the artists.csv is file name and rest is the path where it is to be stored
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #78

Resulting message:

2016-07-12T10:16:33.111+0700 connected to: localhost


2016-07-12T10:16:33.114+0700 exported 13 records

Resulting CSV file:

_id,artistname
ObjectId(5780fbf948ef8c6b3ffb0149),The Tea Party
ObjectId(5781c9ac48ef8c6b3ffb014a),Jorn Lande
1,AC/DC
ObjectId(5781d7f248ef8c6b3ffb014d),The Kooks
ObjectId(5781d7f248ef8c6b3ffb014e),Bastille
ObjectId(5781d7f248ef8c6b3ffb014f),Gang of Four
ObjectId(5781f85d48ef8c6b3ffb0150),Deep Purple
ObjectId(578214f048ef8c6b3ffb0159),Miles Davis
ObjectId(578217c248ef8c6b3ffb015a),Robben Ford
ObjectId(578217c248ef8c6b3ffb015b),Snoop Dogg
2,Prince
3,Moby
4,Rush

Export the results of a Query

You can use the --query option to specify a query to export. The query must be enclosed in single quotes.

Here, we export details on Miles Davis to a JSON file:

$ mongoexport --db mydb --collection artists --query '{"artistname": "Miles Davis"}' --out
home/hduser/17207/miles_davis.json

Resulting message:

2016-07-12T10:32:19.794+0700 connected to: localhost


2016-07-12T10:32:19.795+0700 exported 1 record

Resulting JSON file:

{"_id":{"$oid":"578214f048ef8c6b3ffb0159"},"artistname":"Miles Davis","albums":[{"album":"Kind of
Blue","year":1959.0,"genre":"Jazz"},{"album":"Bitches Brew","year":1970.0,"genre":"Jazz"}]}

Other Options

The mongoexport utility provides a number of options. Here are some potentially useful ones.

The --limit Option

Limits the number of documents in the export.

$mongoexport --db mydb --collection artists --limit 3 --out /home/hduser/17207/3_artists.json


Resulting file:

{"_id":{"$oid":"5780fbf948ef8c6b3ffb0149"},"artistname":"The Tea Party"}


{"_id":{"$oid":"5781c9ac48ef8c6b3ffb014a"},"artistname":"Jorn Lande"}
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #79

{"_id":1.0,"artistname":"AC/DC"}

The --sort Option

Specifies how the results are ordered.

Here, we sort the file by the _id field in ascending order (i.e. 1). To make it descending, use a -1.

mongoexport --db mydb --collection artists --limit 3 --sort '{_id: 1}' --out
/home/hduser/17207/3_artists_sorted.json

Resulting file:

{"_id":1.0,"artistname":"AC/DC"}
{"_id":2.0,"artistname":"Prince","address":{"street":"Audubon
Road","city":"Chanhassen","state":"Minnesota","country":"United States"}}
{"_id":3.0,"artistname":"Moby","albums":[{"album":"Play","year":1999.0,"genre":"Electronica"},
{"album":"Long Ambients 1: Calm. Sleep.","year":2016.0,"genre":"Ambient"}]}

The --skip Option

Allows you to instruct mongoexport to skip a number of documents before starting the export operation.

mongoexport --db mydb --collection artists --limit 3 --sort '{_id: 1}' --skip 2 --out
/home/hduser/17207/3_artists_sorted_skipped.json

Resulting file:

{"_id":3.0,"artistname":"Moby","albums":[{"album":"Play","year":1999.0,"genre":"Electronica"},
{"album":"Long Ambients 1: Calm. Sleep.","year":2016.0,"genre":"Ambient"}]}
{"_id":4.0,"artistname":"Rush"}
{"_id":{"$oid":"5780fbf948ef8c6b3ffb0149"},"artistname":"The Tea Party"}

The --pretty Option

Outputs documents in a more readable JSON format.

$mongoexport --db mydb --collection artists --query '{"artistname": "Miles Davis"}' --pretty --out
/home/hduser/17207/miles_davis_pretty.json

Resulting file:

{
"_id": {
"$oid": "578214f048ef8c6b3ffb0159"
},
"artistname": "Miles Davis",
"albums": [
{
"album": "Kind of Blue",
"year": 1959.0,
"genre": "Jazz"
},
{
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #80

"album": "Bitches Brew",


"year": 1970.0,
"genre": "Jazz"
}
]
}

6.5.16 Automatic Generation of Unique Numbers for the "_id" Field

Step 1: Run the insert () method on a new collection "usercounters". This is to start off with an initial value of 0
for the "seq" field.
db.usercounters.insert( {
_id: "empid",
seq:0 }
)

Step 2: Create a user-defined function "getnextseq". This method will invoke "findAndModify()" method on
the "usercounters" collection. This is to increment the value of seq field by 1 and update the same in
"usercounters" collection.

function getnextseq(name) {
var ret=db.usercounters. findAndModify ( {
query: {_id:name}, update: {$inc:{seq:l}},
new: true }
);
return ret.seq;
}

Step 3: Run the insert() method on the collection where you need to have the "_id" field and get the uniquely
generated number. Notice the call to getnextseq() method as value to _id. The return value from the
getnextseq() method becomes the value of _id.
db.users.insert( {
_id:getnextseq(Mempid"),
Name: "sarah jane" }
)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy