Professional Documents
Culture Documents
Answers1 MongoDBMCAB16
Answers1 MongoDBMCAB16
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”)
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
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)
18. Updates the age and email values of the document with name 'Radhika'.
19. Updates the age as 100, whose age is not greater than 25.
Syntax
db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
db.mycol.remove({'title':'MongoDB Overview'})
db.collection_name.find({},{field_key:1 or 0})
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})
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