MongoDB Practical
MongoDB Practical
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
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}})
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()
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.