MongoDB Class Exercise - 1
MongoDB Class Exercise - 1
db.version()
This will show the version of the mongodb server.
show dbs
This will print a list of the databases present on the server.
use training
This will create a new database named training. If a database named training already
exists, it will start using it.
db.createCollection("mycollection")
This will create a collection name mycollection inside the training database.
show collections
This will print the list of collections in your current database.
Exercise 6 - Insert documents into a
collection
On the mongo client run the below command.
insert() is deprecated
db.mycollection.insert({"color":"white","example":"milk"})
db.mycollection.insertOne({"color":"brown","example":"sugar"})
The above command inserts the json document {"color":"brown","example":"sugar"} into
the collection.
db.mycollection.insertOne({"color":"blue","example":"berry"})
The above command inserts the json document {"color":"blue","example":"berry"} into
the collection.
db.mycollection.count()
db.mycollection. countDocuments()
db.mycollection.find()
This command lists all the documents in the collection mycollection
Notice that mongodb automatically adds an '_id' field to every document in order to
uniquely identify the document.
exit
Practice exercises
1. Problem:
2. Problem:
List databases.
3. Problem:
5. Problem:
List collections
6. Problem:
Insert details of five landmarks including name, city, and country. Example: Eiffel Tower,
Paris, France.
db.landmarks.insertOne({"name":"Big Ben","city":"London","country":"UK"})
db.landmarks.insertOne({"name":"Taj Mahal","city":"Agra","country":"India"})
db.landmarks.insertOne({"name":"Pyramids","country":"Egypt"})
7. Problem:
8. Problem:
9. Problem:
Objectives
After completing this lab, you will be able to:
2. Problem:
3. Problem:
4. Problem:
db.languages.insert({"name":"java","type":"object oriented"})
db.languages.insert({"name":"python","type":"general purpose"})
db.languages.insert({"name":"scala","type":"functional"})
db.languages.insert({"name":"c","type":"procedural"})
db.languages.insert({"name":"c++","type":"object oriented"})
db.languages.countDocuments()
List the first document in the collection.
db.languages.findOne()
List all documents in the collection.
db.languages.find()
List first 3 documents in the collection.
db.languages.find().limit(3)
Query for "python" language.
db.languages.find({"name":"python"})
Query for "object oriented" languages.
db.languages.find({"type":"object oriented"})
List only specific fields.
Using a projection document you can specify what fields we wish to see or skip in the
output.
This command lists all the documents with only name field in the output.
db.languages.find({},{"name":1})
This command lists all the documents without the name field in the output.
db.languages.find({},{"name":0})
This command lists all the "object oriented" languages with only "name" field in the
output.
db.languages.find({"type":"object oriented"},{"name":1})
db.languages.updateMany({},{$set:{"description":"programming language"}})
Set the creator for python language.
db.languages.updateMany({"type":"object oriented"},{$set:{"compiled":true}})
db.languages.remove({"name":"scala"})
Delete the object oriented languages.
db.languages.remove({"type":"object oriented"})
Delete all the documents in a collection.
db.languages.remove({})
Practice exercises
Run the below code on mongo console. It will insert 5 documents, which will serve as
sample data for the next steps.
use training
db.languages.insert({"name":"java","type":"object oriented"})
db.languages.insert({"name":"python","type":"general purpose"})
db.languages.insert({"name":"scala","type":"functional"})
db.languages.insert({"name":"c","type":"procedural"})
db.languages.insert({"name":"c++","type":"object oriented"})
1. Problem:
Insert an entry for 'Haskell' programming language which is of type 'functional .
2. Problem:
3. Problem:
4. Problem:
5. Problem: