Professional Documents
Culture Documents
Experiment No. 5
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
If you want to check your databases list, use the command show dbs.
>show dbs
Syntax
db.COLLECTION_NAME.drop()
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)
Results: (Screen shots of application development steps, program code and web browser
displaying the specified message.)
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> 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>
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},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 }
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
},
…
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
}
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.
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
References:
1. By Isaac Strack; “Getting Started with Meteor.js JavaScript Framework”, 2nd Edition
;Packt Publishing, June 2015