Practical 1
Aim:- MongoDB Basics
1a. Write a MongoDB query to create and drop database.
Syntax:
Create -
use database_name
Drop -
db.dropDatabase()
1b. Write a MongoDB query to create, display and drop collection.
Syntax:
Create -
db.createCollection(collection_name)
db.collection_name.insert({key:value})
Display -
db.collection_name.find()
Drop -
db.collection_name.drop()
Source code:
use tyit
db.createCollection("students")
db.students.insert({Name:"Arman", RollNo:"13", Class:"A", Gender:"M"})
show collections
db.students.find()
db.students.drop()
1c. Write a MongoDB query to insert, query, update and delete a document.
Syntax:
To Insert Document:
db.COLLECTION_NAME.insert(document)
To Update Document:
db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED DATA)
To Delete Document:
db.COLLECTION_NAME.remove(DELETION_CRITERIA)
Practical 2
SIMPLE QUERIES WITH MONGODB
1) Selector -
Syntax:
db.collection_name.find({"Key":"Value"})
Source code:
Use tyit
db.student.find({"Gender":"M"})
2) Projector -
Syntax:
db.collection_name.find({"Key":"Value"}, {"Key":Value,"Key":Value})
Source code:
use tyit
db.student.find({"Gender":"M"},{"Name":1,"Age":1})
3) sort()
Syntax:
db.collection_name.find({"Key":"Value"}, {"Key":Value,"Key":Value}).sort({"Key":Value})
Source code:
Use tyit
db.student.find({Gender:”F”},{Name:1,Age:1}).sort({Age:1}) //follows ascending order
db.student.find({Gender:”F”},{Name:1,Age:1}).sort({Age:-1} //follows descending order
4) Limit()- number of doc to display
Syntax:
db.collection_name.find({"Key":"Value", $or:[{"Key":"Value"), {"Key":" Value"}]}).limit(Value)
Source code:
Use tyit
db.stud.find({("gender":"F", $or:[{"class":"a"},{"score":"70"} ]}).limit(2)
5) Skip() -
Syntax:
db.collection_name.find({"Key":"Value", $or:[{"Key":"Value"},
{"Key":"Value"}]}).limit(Value).skip(Value)
Source code:
Use tyit
db.stud.find(("Gender":"F", $or:[{"class":"a"}, {"score":"70"}]}).limit(2).skip(2)
6) Findone() -
Syntax:
db.collection_name.findOne({"Key":"Value"}, {"Key":Value,"Key":Value})
db.student.findOne({“gender”:”F”},{name:1,Age:1})
db.collection_name.findOne()
7) ensurelndex
Syntax:
db.collection_name.ensureIndex({"Key":"Value"})
Source Code:
Use mydatabase
Show collections
db.example.ensurelndex({"age":26})
8) Pretty() -
Syntax:
db.collection_name.find().pretty()
Source Code:
use tyit
show collections
db.user1.find() db.user1.find.pretty()
9) Conditional Operators -
Syntax:
$lt and $lte
db.collection_name.find({"Key":{"$lt":Value}})
db.collection_name.find({"Key":{"$lte":Value}})
$gt and $gte
db.collection_name.find({"Key":{"$gt":Value}})
db.collection_name.find({"Key":{"$gte":Value}})
$in and $nin
db.collection_name.find({"Key":{"$in":["Value","Value"]}})
db.collection_name.find({"Key":{"$nin":["Value","Value"]}})
Source code:
use tyit
use tyit
db.createCollection("stud")
db.stud.insert({Name:"S1", Age:25, Gender:"M", Class:"C1", Score:95})
db.stud.insert({Name:"S2", Age: 18, Gender:"M", Class:"C1", Score:85})
db.stud.insert({Name:"S3", Age: 18, Gender:"F", Class:"C1", Score:85})
db.stud.insert({Name:"S4", Age: 18, Gender:"F", Class:"C1", Score:75})
db.stud.insert({Name:"S5", Age: 18, Gender:"F", Class:"C2", Score:75))
db.stud.insert({Name: "S6", Age:21, Gender:"M", Class:"C2", Sc ore:100})
db.stud.insert({Name:"S7", Age:21, Gender:"M", Class:"C2", Sc ore:100})
db.stud.insert({Name:"S8", Age:25, Gender:"F", Class:"C2", Sco re:100})
db.stud.insert({Name:"S9", Age:25, Gender:"F", Class:"C2", Score:90})
db.stud.insert({Name:"S10", Age:28, Gender:"F", Class:"C3", Sc ore:90})
db.stud.find()
$lt and $lte
db.stud.find({"Age":{"$lt":25}})
db.stud.find({"Age":{"$lte":25}})
$gt and $gte
gte db.stud.find({"Age":{"$gt":25}})
db.stud.find({"Age":{"$gte":25}})
$in and $nin
db.stud.find({"Class":{"$in":["C1","C2"]}})
db.stud.find({"Class":{"$nin":["C1","C2"]}})
Aim:- Implementing Aggregation
2a. Write a MongoDB query to use some average, mini and Max expression.
Syntax:
db.collection_name.aggregation([{$group:{id:”Column_to_be_group”,key:{$aggregation_function:
$value}}}])
2b. Write a MongoDB query to use push and addtoset expression.
2.c Write a MongoDB query to use first and last expression.
Practical 3
Aim:- Replication Backup and Restore
3a. Write a MongoDB query to create a Replica of existing database.
3b. Write a MongoDB query to create a backup of existing database.
3c. Write a MongoDB query to Restore database from the backup.