Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

1.

1. use myfirstdb
switched to db myfirstdb

2.
use mydb
switched to db mydb

show dbs

db.dropdatabase()
4. Create a collection called Mycollection and Mycol
5. check the available collections into your database mydb

db.createcollection(“Mycollection”)
db.createcollection(“mydb”)

6. Drop the collection with the name mycollection.

db. Mycollection.drop()
db.mycol.insert
To display the documents from Mycol in a formatted way by using pretty ()
method.
db.mycol.find().pretty()
Display only one document from Mycol.
db.mycol.findone()
To show all the tutorials written by ‘ Karl’ and whose title is 'MongoDB Overview'.
db.mycol.find({ $and: [ {“by” :”Karl”},{“title”:”MongoDB Overview”}]}).pretty()
To show all the tutorials written by 'Karl' or whose title is 'MongoDB Overview'.
db.mycol.find({ $or: [ {“by” :”Karl”},{“title”:”MongoDB Overview”}]}).pretty()
Display the documents that have likes greater than 10 and whose title is either
'MongoDB Overview' or by is 'Karl’.
db.mycol.find({ “likes”: { $gt: 10} , $or: [ {“by” :”Karl”},{“title”:”MongoDB
Overview”}]}).pretty()
1. Retrieve the document(s) from empdetails whose first name is not "Radhika" and last
name is not "Christopher".

db.empDetails.find(
{
$not:[
{"First_Name": "Radhika"},
{"Last_Name": "Christopher"}
]
}
).pretty()
Retrieve the document(s) from empdetails whose age is not greater than 25

db.empDetails.find( { "Age": { $not: { $gt: "25" } } })

Retrieve the document(s) from empdetails whose age is in the list {24,25,26}
db.empDetails.find( { "Age": { $in: {"25”,”24”,”26” } } })
17. Set the new title 'New MongoDB Tutorial' of the documents whose title is 'MongoDB Overview'.

Syntax
db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

db.mycol.update({'title':'MongoDB Overview'}, {$set:{'title':'New MongoDB Tutorial'}},{multi:true})

18. Updates the age and email values of the document with name 'Radhika'.

db.empDetails.findOneAndUpdate( {First_Name: 'Radhika'}, { $set:


{ Age: '30',e_mail: 'radhika_newemail@gmail.com'}} )

19. Updates the age as 100, whose age is not greater than 25.

db.empDetails.updateMany( {Age:{ $gt: "25" }}, { $set: { Age:


‘100'}} )
20. Remove all the documents whose title is 'MongoDB Overview'.

Syntax

db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

db.mycol.remove({'title':'MongoDB Overview'})

21. Retrieve student id from all the documents.


Syntax

db.collection_name.find({},{field_key:1 or 0})

db.studentdata.find({}, {"_id": 0, "student_id": 1})

22. List of all the students, having the id > 2002.

db.studentdata.find({student_id : {$gt:2002}}).pretty()
23. List only one students, having the id > 2002.

db.studentdata.find({student_id : {$gt:2002}}).limit(1).pretty()
24. List the second record of the students, having the id > 2002

db.studentdata.find({student_id : {$gt:2002}}).limit(1).skip(1).pretty()
25. Display the student_id of all the documents in descending
order.

Syntax
db.collecttion_name.find().sort({field_key:1 or -1})

db.studentdata.find({}, {"student_id": 1, _id:0}).sort({"student_id": -1})

26. Display the student_id field of all the students


in ascending order.
db.studentdata.find({}, {"student_id": 1, _id:0}).sort({"student_id": 1})
27. Sort the documents based on student_id and display the student_age and
student_name fields.
28.create the index on student_name field in ascending order.
29. find the indexes of studentdata collection.
30 . Drop the index that we have created on student_name field in the
collection studentdata.
27.To create backup of database and to restore backup data.
31.
db.studentdata.find({}, {"student_id": 0, _id:0}).sort({"student_id": 1})

Syntax (Index)
db.collection_name.createIndex({field_name: 1 or -1})

28.
db.studentdata.createIndex({student_name: 1})
29.
db.collection_name.getIndexes()

30.
db.collection_name.dropIndex({index_name: 1})
31.
mongodump
mongorestore

You might also like