You are on page 1of 11

KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

Experiment No. 5

Title: MongoDB crud operation.

(Autonomous College Affiliated to University of Mumbai)


KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

Batch:A1 Roll No.:1614012 Experiment No.:5

Aim: Perform crude operation of MongoDB.


--------------------------------------------------------------------------------------------

Resources needed: Meteor

Theory:
MongoDB is a cross-platform, document oriented database that provides, high performance,
high availability, and easy scalability. MongoDB works on concept of collection and
document.
1. The use Command
In MongoDB use DATABASE_NAME is used to create database. The command will create a
new database if it doesn't exist, otherwise it will return the existing database.
Syntax
use DATABASE_NAME

To check your currently selected database, use the command db


>db

If you want to check your databases list, use the command show dbs.
>show dbs

2. The dropDatabase() Method


MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax
db.dropDatabase()
This will delete the selected database.

3. The createCollection() Method


MongoDB db.createCollection(name, options) is used to create collection.
Syntax
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and is used to
specify configuration of collection.
Parameter Type Description

Name String Name of the collection to be created

Options Document (Optional) Specify options about memory


size and indexing

4. The drop() Method


MongoDB's db.collection.drop() is used to drop a collection from the database.

(Autonomous College Affiliated to University of Mumbai)


KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

Syntax

db.COLLECTION_NAME.drop()

5. The insert() Method


To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.
Syntax
>db.COLLECTION_NAME.insert(document

6. The find() Method


To query data from MongoDB collection, you need to use MongoDB's find() method.
Syntax
>db.COLLECTION_NAME.find()
find() method will display all the documents in a non-structured way.

7. The pretty() Method


To display the results in a formatted way, you can use pretty() method.
Syntax
>db.mycol.find().pretty()

8. findOne()
Apart from find() method, there is findOne() method, that returns only one document.

9. Update() Method
The update() method updates the values in the existing document.
Syntax
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

10. Save() Method


The save() method replaces the existing document with the new document passed in the save()
method.
Syntax
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

11. The remove() Method


MongoDB's remove() method is used to remove a document from the collection. remove()
method accepts two parameters. One is deletion criteria and second is justOne flag.
 deletion criteria − (Optional) deletion criteria according to documents will be removed.
 justOne − (Optional) if set to true or 1, then remove only one document.
Syntax
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

(Autonomous College Affiliated to University of Mumbai)


KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

-Remove Only One


If there are multiple records and you want to delete only the first record, then
set justOne parameter in remove() method.
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)

-Remove All Documents


If you don't specify deletion criteria, then MongoDB will delete whole documents from the
collection. This is equivalent of SQL's truncate command.
>db.mycol.remove()

Results: (Screen shots of application development steps, program code and web browser
displaying the specified message.)

Creating db and collection

meteor:PRIMARY> use jashdb


switched to db jashdb
meteor:PRIMARY> db.createCollection("profile")
{ "ok" : 1 }

Insert operations using save function

meteor:PRIMARY> db.profile.save({name:"Jash
Gopani",email:"jash@gmail.com",college:"KJSCE",age:21})
WriteResult({ "nInserted" : 1 })
meteor:PRIMARY> db.profile.save({name:"Chetan
Kachaliya",email:"chetan@gmail.com",college:"KJSCE",age:22})
WriteResult({ "nInserted" : 1 })

meteor:PRIMARY> for(var i=0;i<20;i++){


...
db.profile.save({name:"student"+i,email:"student"+i+"@gmail.com",colle
ge:"KJSCE",age:Math.floor(Math.random()*25)});
... }
WriteResult({ "nInserted" : 1 })

(Autonomous College Affiliated to University of Mumbai)


KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

Read Operation using find()

meteor:PRIMARY> db.profile.find()
{ "_id" : ObjectId("5cb04dab8fb0ea56630096bb"), "name" : "Jash
Gopani", "email" : "jash@gmail.com", "college" : "KJSCE", "age" : 21 }
{ "_id" : ObjectId("5cb04de48fb0ea56630096bc"), "name" : "Chetan
Kachaliya", "email" : "chetan@gmail.com", "college" : "KJSCE", "age" :
22 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096bd"), "name" : "student0",
"email" : "student0@gmail.com", "college" : "KJSCE", "age" : 12 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096be"), "name" : "student1",
"email" : "student1@gmail.com", "college" : "KJSCE", "age" : 3 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096bf"), "name" : "student2",
"email" : "student2@gmail.com", "college" : "KJSCE", "age" : 13 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c0"), "name" : "student3",
"email" : "student3@gmail.com", "college" : "KJSCE", "age" : 22 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c1"), "name" : "student4",
"email" : "student4@gmail.com", "college" : "KJSCE", "age" : 5 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c2"), "name" : "student5",
"email" : "student5@gmail.com", "college" : "KJSCE", "age" : 18 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c3"), "name" : "student6",
"email" : "student6@gmail.com", "college" : "KJSCE", "age" : 5 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c4"), "name" : "student7",
"email" : "student7@gmail.com", "college" : "KJSCE", "age" : 15 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c5"), "name" : "student8",
"email" : "student8@gmail.com", "college" : "KJSCE", "age" : 13 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c6"), "name" : "student9",
"email" : "student9@gmail.com", "college" : "KJSCE", "age" : 18 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c7"), "name" : "student10",
"email" : "student10@gmail.com", "college" : "KJSCE", "age" : 9 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c8"), "name" : "student11",
"email" : "student11@gmail.com", "college" : "KJSCE", "age" : 5 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c9"), "name" : "student12",
"email" : "student12@gmail.com", "college" : "KJSCE", "age" : 3 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096ca"), "name" : "student13",
"email" : "student13@gmail.com", "college" : "KJSCE", "age" : 21 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096cb"), "name" : "student14",
"email" : "student14@gmail.com", "college" : "KJSCE", "age" : 16 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096cc"), "name" : "student15",
"email" : "student15@gmail.com", "college" : "KJSCE", "age" : 17 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096cd"), "name" : "student16",
"email" : "student16@gmail.com", "college" : "KJSCE", "age" : 9 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096ce"), "name" : "student17",
"email" : "student17@gmail.com", "college" : "KJSCE", "age" : 5 }
Type "it" for more
meteor:PRIMARY>

(Autonomous College Affiliated to University of Mumbai)


meteor:PRIMARY> db.profile.find().pretty() KJSCE/IT/TYBTECH/SEM VI/DF/2018-19
{
"_id" : ObjectId("5cb04dab8fb0ea56630096bb"),
"name" : "Jash Gopani",
"email" : "jash@gmail.com",
"college" : "KJSCE",
"age" : 21
}
{
"_id" : ObjectId("5cb04de48fb0ea56630096bc"),
"name" : "Chetan Kachaliya",
"email" : "chetan@gmail.com",
"college" : "KJSCE",
"age" : 22
}
{
"_id" : ObjectId("5cb04f248fb0ea56630096bd"),
"name" : "student0",
"email" : "student0@gmail.com",
"college" : "KJSCE",
"age" : 12
}
{
"_id" : ObjectId("5cb04f248fb0ea56630096be"),
"name" : "student1",
"email" : "student1@gmail.com",
"college" : "KJSCE",
"age" : 3
}
{
"_id" : ObjectId("5cb04f248fb0ea56630096c9"),
"name" : "student12",
"email" : "student12@gmail.com",
"college" : "KJSCE",
"age" : 3
}
{
"_id" : ObjectId("5cb04f248fb0ea56630096ca"),
"name" : "student13",
"email" : "student13@gmail.com",
"college" : "KJSCE",
"age" : 21
}
{
"_id" : ObjectId("5cb04f248fb0ea56630096cb"),
"name" : "student14",
"email" : "student14@gmail.com",
"college" : "KJSCE",
"age" : 16
}

(Autonomous College Affiliated to University of Mumbai)


KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

Using operators

meteor:PRIMARY> db.profile.find({age:{$eq:18}})
{ "_id" : ObjectId("5cb04f248fb0ea56630096c2"), "name" : "student5",
"email" : "student5@gmail.com", "college" : "KJSCE", "age" : 18 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c6"), "name" : "student9",
"email" : "student9@gmail.com", "college" : "KJSCE", "age" : 18 }

meteor:PRIMARY> db.profile.find({age:{$gt:18, $lt:25}})


{ "_id" : ObjectId("5cb04dab8fb0ea56630096bb"), "name" : "Jash
Gopani", "email" : "jash@gmail.com", "college" : "KJSCE", "age" : 21 }
{ "_id" : ObjectId("5cb04de48fb0ea56630096bc"), "name" : "Chetan
Kachaliya", "email" : "chetan@gmail.com", "college" : "KJSCE", "age" :
22 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c0"), "name" : "student3",
"email" : "student3@gmail.com", "college" : "KJSCE", "age" : 22 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096ca"), "name" : "student13",
"email" : "student13@gmail.com", "college" : "KJSCE", "age" : 21 }

meteor:PRIMARY> db.profile.find({age:{$gt:18,
$lt:25},college:{$eq:"KJSCE"}})
{ "_id" : ObjectId("5cb04dab8fb0ea56630096bb"), "name" : "Jash
Gopani", "email" : "jash@gmail.com", "college" : "KJSCE", "age" : 21 }
{ "_id" : ObjectId("5cb04de48fb0ea56630096bc"), "name" : "Chetan
Kachaliya", "email" : "chetan@gmail.com", "college" : "KJSCE", "age" :
22 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c0"), "name" : "student3",
"email" : "student3@gmail.com", "college" : "KJSCE", "age" : 22 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096ca"), "name" : "student13",
"email" : "student13@gmail.com", "college" : "KJSCE", "age" : 21 }

(Autonomous College Affiliated to University of Mumbai)


KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

Using regular expressions to find records

meteor:PRIMARY> res =
db.profile.find({name:{$regex:/(^student)/}}).toArray()
[
{
"_id" : ObjectId("5cb04f248fb0ea56630096bd"),
"name" : "student0",
"email" : "student0@gmail.com",
"college" : "KJSCE",
"age" : 12
},
{
"_id" : ObjectId("5cb04f248fb0ea56630096be"),
"name" : "student1",
"email" : "student1@gmail.com",
"college" : "KJSCE",
"age" : 3
},
{
"_id" : ObjectId("5cb04f248fb0ea56630096bf"),
"name" : "student2",
"email" : "student2@gmail.com",
"college" : "KJSCE",
"age" : 13
},
{
"_id" : ObjectId("5cb04f248fb0ea56630096c0"),
"name" : "student3",
"email" : "student3@gmail.com",
"college" : "KJSCE",
"age" : 22
},
{
"_id" : ObjectId("5cb04f248fb0ea56630096c1"),
"name" : "student4",
"email" : "student4@gmail.com",
"college" : "KJSCE",
"age" : 5
},
{
"_id" : ObjectId("5cb04f248fb0ea56630096c2"),
"name" : "student5",
"email" : "student5@gmail.com",
"college" : "KJSCE",
"age" : 18
},

(Autonomous College Affiliated to University of Mumbai)


KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

Displaying stats of database

meteor:PRIMARY> db.profile.stats({scale:10})
{
"ns" : "jashdb.profile",
"count" : 22,
"size" : 246,
"avgObjSize" : 112,
"numExtents" : 1,
"storageSize" : 819,
"lastExtentSize" : 819.2,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and
unmaintained in 3.0. It remains hard coded to 1.0 for compatibility
only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 1,
"totalIndexSize" : 817,
"indexSizes" : {
"_id_" : 817
},
"ok" : 1
}

Batch Update operation

meteor:PRIMARY>
db.profile.updateMany({name:{$regex:/(^student)/}},{$set:{name:"replac
edByREGEX-"+Math.floor(Math.random()*100)}})
{ "acknowledged" : true, "matchedCount" : 20, "modifiedCount" : 20 }
meteor:PRIMARY> db.profile.find({name:{$regex:/(^replacedByREGEX)/}})
{ "_id" : ObjectId("5cb04f248fb0ea56630096bd"), "name" :
"replacedByREGEX-62", "email" : "student0@gmail.com", "college" :
"KJSCE", "age" : 12 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096be"), "name" :
"replacedByREGEX-62", "email" : "student1@gmail.com", "college" :
"KJSCE", "age" : 3 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096bf"), "name" :
"replacedByREGEX-62", "email" : "student2@gmail.com", "college" :
"KJSCE", "age" : 13 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c0"), "name" :
"replacedByREGEX-62", "email" : "student3@gmail.com", "college" :
"KJSCE", "age" : 22 }
{ "_id" : ObjectId("5cb04f248fb0ea56630096c1"), "name" :
"replacedByREGEX-62", "email" : "student4@gmail.com", "college" :
"KJSCE", "age" : 5 }
. . . (Autonomous College Affiliated to University of Mumbai)
KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

____________________________________________________________________________________

Questions:
1. Explain features of MongoDB in detail.
Ans: Some of the key features of MongoDB are
a) Aggregation framework : The aggregation framework enables users to obtain the kind
of results for which the SQL GROUP BY clause is used. This MapReduce can be used
to parallelize, allow huge data for processing over lots of cores/machines
b) BSON format : BSON (Binary JSON) format enables MongoDB to internally index
and map document properties and even nested documents. It is designed to be more
efficient in size and speed, allowing MongoDB’s high read/write throughput.
c) Sharding : Sharding is a method for distributing data across multiple machines.
MongoDB uses sharding to support deployments with very large data sets and high
throughput operations.
d) Adhoc queries : MongoDB supports field, range queries, regular expression searches.
Queries can return specific fields of documents and also include user-defined
JavaScript functions. MongoDB is able to support ad hoc queries by indexing BSON
documents and using a unique query language.
e) Schema-Less : MongoDB is a schema-less database (written in C++) because of which
is much more flexible than traditional database tables. The benefit is the lack of setup
and the reduced friction with OOP. So, in order to save an object, you just have to
serialize it to JSON and send it to MongoDB. There is no need for type mapping which
removes an additional burden.
f) Capped Collection : MongoDB supports fixed-size collections called capped
collections. This type of collection maintains insertion order. Once the specified size
has been reached, it starts behaving like a circular queue.
g) Indexing : Any field in a MongoDB document can be indexed with primary and
secondary indices. It enables the database engine to efficiently resolve queries which
make it one of the best key features of MongoDB. The database engine can use a
predefined index, which maps documents fields and can tell the engine which
documents are compatible with this query statement, hence improves performance.
h) File Storage : MongoDB can be used as a file system with load balancing and data
replication features over multiple machines for storing files. This function, called Grid
File System, is included with MongoDB drivers which stores files. MongoDB exposes
functions for file manipulation and content to developers.
i) Replication : MongoDB provides replication feature by distributing data across
different machines. It can have one primary node and one or more secondary nodes.
This typology is known as replica set. It also has a automatic failover feature which
enables the secondary site when the primary fails and then re connects to the primary
when it comes back online
j) Management service : MMS is a powerful web tool that allows us tracking our
databases and our machines and also backing up our data. MMS also tracks hardware
metrics for managing a MongoDB deployment. It shows performance in a rich web
console to help you optimize your deployment. It also provides features of custom
alerts which helps to discover issues before your MongoDB instance will be affected.

(Autonomous College Affiliated to University of Mumbai)


KJSCE/IT/TYBTECH/SEM VI/DF/2018-19

Outcomes:
Understand the need, features, architecture and applications of frameworks
Conclusion: (Conclusion to be based on the objectives and outcomes achieved)
In this experiment we performed CRUD operations on MongoDB on the command line. The
data stored in MongoDB has no specific structure like traditional relational databases. Here
data is stored in form of collections and documents and the queries used for performing
operations are made up of methods provided by MongoDB unlike long SQL queries.

Grade: AA / AB / BB / BC / CC / CD/DD

Signature of faculty in-charge with date

References:

1. By Isaac Strack; “Getting Started with Meteor.js JavaScript Framework”, 2nd Edition
;Packt Publishing, June 2015

(Autonomous College Affiliated to University of Mumbai)

You might also like