Set1:
1. Create a new database called recipes and a collection called indian_dishes.
2. Insert 2 new documents into the indian_dishes collection, each with the fields name, description, ingredients, and instructions.
3. Query the indian_dishes collection and retrieve all documents.
4. Query the indian_dishes collection and retrieve only the name and description fields of all documents.
5. Update the document for a specific dish by its name field to change its description.
Solution:
use recipes
db.createCollection("indian_dishes")
db.indian_dishes.insertMany([ { "name": "Butter Chicken", "description": "A classic North Indian dish made with chicken and tomato-based gravy", "ingredients": ["chicken", "butter", "tomatoes", "cream"],
"instructions": "1. Marinate chicken. 2. Cook chicken in butter. 3. Make gravy with tomatoes and cream. 4. Add cooked chicken to gravy and serve."
},
{
"name": "Palak Paneer",
"description": "A popular vegetarian dish made with spinach and cottage cheese",
"ingredients": ["spinach", "paneer", "onions", "tomatoes"],
"instructions": "1. Blanch spinach. 2. Make gravy with onions and tomatoes. 3. Add spinach and paneer to gravy. 4. Serve hot."
}
])
o/p:
{ "_id" : ObjectId("6061d5a55a7f2eaae1fe139a"), "name" : "Butter Chicken", "description" : "A classic North Indian dish made with chicken and tomato-based gravy", "ingredients" : [ "chicken", "butter", "tomatoes", "cream" ], "instructions" : "1. Marinate chicken. 2. Cook chicken in butter. 3. Make gravy with tomatoes and cream. 4. Add cooked chicken to gravy and serve." }
{ "_id" : ObjectId("6061d5a55a7f2eaae1fe139b"), "name" : "Palak Paneer", "description" : "A popular vegetarian dish made with spinach and cottage cheese", "ingredients" : [ "spinach", "paneer", "onions", "tomatoes" ], "instructions" : "1. Blanch spinach. 2. Make gravy with onions and tomatoes. 3. Add spinach and paneer to gravy. 4. Serve hot." }
db.indian_dishes.find({}, {"name": 1, "description": 1, "_id": 0})
o/p:
{ "name" : "Butter Chicken", "description" : "A classic North Indian dish made with chicken and tomato-based gravy" }
{ "name" : "Palak Paneer", "description" : "A popular vegetarian dish made with spinach and cottage cheese" }
db.indian_dishes.updateOne({"name": "Butter Chicken"}, {$set: {"description": "A classic North Indian dish made with chicken and creamy tomato-based gravy"}})
o/p:
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
Set2:
1.Create a new database called movies and a collection called actors.
2. Insert 4 new documents into the actors collection, each with the fields name, birthdate, nationality, and movies.
3. Query the actors collection and retrieve all documents.
4. Query the actors collection and retrieve only the name and nationality fields of all documents.
5. Update the document for a specific actor by its name field to change its birthdate.
O/p:
use movies
db.createCollection("indian_actors")
db.indian_actors.insertMany([ { "name": "Amitabh Bachchan", "birthdate": "11 October 1942", "nationality": "Indian", "movies": ["Zanjeer", "Deewar", "Sholay"]
},
{
"name": "Shah Rukh Khan",
"birthdate": "2 November 1965",
"nationality": "Indian",
"movies": ["Dilwale Dulhania Le Jayenge", "Kuch Kuch Hota Hai", "Chennai Express"]
},
{
"name": "Aamir Khan",
"birthdate": "14 March 1965",
"nationality": "Indian",
"movies": ["Lagaan", "Taare Zameen Par", "Dangal"]
},
{
"name": "Rajkummar Rao",
"birthdate": "31 August 1984",
"nationality": "Indian",
"movies": ["Shahid", "Newton", "Stree"]
}
])
db.indian_actors.find({})
o/p:
{ "_id" : ObjectId("606e04c8e17f680c646ee7b4"), "name" : "Amitabh Bachchan", "birthdate" : "11 October 1942", "nationality" : "Indian", "movies" : [ "Zanjeer", "Deewar", "Sholay" ] }
{ "_id" : ObjectId("606e04c8e17f680c646ee7b5"), "name" : "Shah Rukh Khan", "birthdate" : "2 November 1965", "nationality" : "Indian", "movies" : [ "Dilwale Dulhania Le Jayenge", "Kuch Kuch Hota Hai", "Chennai Express" ] }
{ "_id" : ObjectId("606e04c8e17f680c646ee7b6"), "name" : "Aamir Khan", "birthdate" : "14 March 1965", "nationality" : "Indian", "movies" : [ "Lagaan", "Taare Zameen Par", "Dangal" ] }
{ "_id" : ObjectId("606e04c8e17f680c646ee7b7"), "name" : "Rajkummar Rao", "birthdate" : "31 August 1984", "nationality" : "Indian", "movies" : [ "Shahid", "Newton", "Stree" ] }
db.indian_actors.find({}, { "name": 1, "nationality": 1, "_id": 0 })
o/p:
{ "name" : "Amitabh Bachchan", "nationality" : "Indian" }
{ "name" : "Shah Rukh Khan", "nationality" : "Indian" }
{ "name" : "Aamir Khan", "nationality" : "Indian" }
{ "name" : "Kajol", "nationality" : "Indian" }
db.indian_actors.updateOne({name: "Amitabh Bachchan"}, {$set: {birthdate: ISODate("1942-10-11")}})
:
db.indian_actors.find({name: "Amitabh Bachchan"})
O/p:
{ "_id" : ObjectId("6069c15705fcb0a1b2d14b51"), "name" : "Amitabh Bachchan", "birthdate" : ISODate("1942-10-11T00:00:00Z"), "nationality" : "Indian", "movies" : [ "Sholay", "Deewar", "Zanjeer" ] }
set 3:
1. Create a new database called store and a collection called indian_customers.
2. Insert 3 new documents into the customers collection, each with the fields name, email, phone, and address.
3. Query the customers collection and retrieve all documents.
4. Query the customers collection and retrieve only the name and email fields of all documents.
5. Update the document for a specific customer by its email field to change its phone
// Create the store database
use store
// Create the indian_customers collection and insert 3 new documents
db.indian_customers.insertMany([
{
name: "Rajesh Sharma",
email: "rajesh@example.com",
phone: "555-1234",
address: "123 Main St."
},
{
name: "Neha Gupta",
email: "neha@example.com",
phone: "555-5678",
address: "456 Oak Ave."
},
{
name: "Sanjay Patel",
email: "sanjay@example.com",
phone: "555-9012",
address: "789 Elm St."
}
])
// Query the indian_customers collection and retrieve all documents
db.indian_customers.find()
// Query the indian_customers collection and retrieve only the name and email fields of all documents
db.indian_customers.find({}, {name: 1, email: 1, _id: 0})
// Update the phone of a specific customer document by its email field
db.indian_customers.updateOne({email: "rajesh@example.com"}, {$set: {phone: "555-4321"}})
// Confirm the update
db.indian_customers.find({email: "rajesh@example.com"})
O/p:
similar to set 2 output
set 4:
1. Create an index on the name field of a collection called students. create 3 documents in student collection with fields name, age, semester and degree
2. Query the students collection and retrieve all documents sorted by the name field in ascending order.
3. Query the students collection and retrieve only the documents where the name field contains the word "John".
4. Create a compound index on the name and age fields of the students collection.
5. Query the students collection and retrieve only the documents where the name field contains the word "John" and the age field is greater than or equal to 18
Solution:
db.students.createIndex({"name": 1})
also insert 3 documents as per q.1
db.students.find().sort({"name": 1})
o/p;
{ "_id" : ObjectId("..."), "name" : "John v v", "age" : 20, "semester" : 2, "degree" : "Computer Science" }
{ "_id" : ObjectId("..."), "name" : "Mary", "age" : 22, "semester" : 4, "degree" : "Business" }
{ "_id" : ObjectId("..."), "name" : "Tom", "age" : 19, "semester" : 3, "degree" : "Engineering" }
db.students.find({"name": /John/})
o/p:
{ "_id" : ObjectId("..."), "name" : "John", "age" : 20, "semester" : 2, "degree" : "Computer Science" }
db.students.createIndex({"name": 1, "age": 1})
db.students.find({"name": /John/, "age": {"$gte": 18}})
o/p:
{ "_id" : ObjectId("..."), "name" : "John", "age" : 20, "semester" : 2, "degree" : "Computer Science" }
set 5:
Write a Python program in Jupyter to create following documents in MongoDB
DB: Sports
Collection: New
Documents:
{ name: cricket, level:international, no:11, players:{batsmen:5,bowlers:3,Allrounders:2, WK:1}
{ name: hockey, level:national, no:10, players:{GK:1,Offense:4,defense:5}
Solution:
pip install pymongo
import pymongo
# Establish connection to MongoDB
client = pymongo.MongoClient('mongodb://localhost:27017/')
# Create database and collection
db = client['Sports']
collection = db['New']
# Create documents
doc1 = {
'name': 'cricket',
'level': 'international',
'no': 11,
'players': {
'batsmen': 5,
'bowlers': 3,
'Allrounders': 2,
'WK': 1
}
}
doc2 = {
'name': 'hockey',
'level': 'national',
'no': 10,
'players': {
'GK': 1,
'Offense': 4,
'defense': 5
}
}
# Insert documents into collection
collection.insert_one(doc1)
collection.insert_one(doc2)
set 6:
Write a Python program in Jupyter to create following documents in MongoDB
DB: Sports
Collection: New
Documents:
{ name: cricket, level:international, no:11}
{ name: hockey, level:national, no:10}
DB: students
Collection: col1
Documents:
{ name: sudheer, degree:BTech, sem:6, subjects:[s1,s2,s3] }
{ name: Rakesh, degree:BTech, sem:5, subjects:[s1,s2] }
O/P:
check in Compass or Mongoshell
pip install pymongo (in a cell)
import pymongo
# Connect to MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")
# Create the Sports database
db = client["Sports"]
# Create the New collection
collection = db["New"]
# Insert the cricket document
cricket_doc = {"name": "cricket", "level": "international", "no": 11, "players": {"batsmen": 5, "bowlers": 3, "Allrounders": 2, "WK": 1}}
collection.insert_one(cricket_doc)
# Insert the hockey document
hockey_doc = {"name": "hockey", "level": "national", "no": 10, "players": {"GK": 1, "Offense": 4, "defense": 5}}
collection.insert_one(hockey_doc)
# Create the students database
db = client["students"]
# Create the col1 collection
collection = db["col1"]
# Insert the sudheer document
sudheer_doc = {"name": "sudheer", "degree": "BTech", "sem": 6, "subjects": ["s1", "s2", "s3"]}
collection.insert_one(sudheer_doc)
# Insert the Rakesh document
rakesh_doc = {"name": "Rakesh", "degree": "BTech", "sem": 5, "subjects": ["s1", "s2"]}
collection.insert_one(rakesh_doc)
o/p: check in mongo shell or Compass
set 7:
Create a new database called "store" and a collection called "products".
Insert a new document into the "products" collection with the fields "name", "price", "quantity", and "category".
Insert 3 new documents into the "products" collection.
Query the "products" collection and retrieve all documents.
Query the "products" collection and retrieve only the "name" and "category" fields of all documents.
Update the document for a specific product by its "name" field to change its "quantity".
solution:
use store
db.products.insertOne({name: "Product 1", price: 10, quantity: 100, category: "Category 1"})
o/p:
{
"acknowledged" : true,
"insertedId" : ObjectId("6257a1e27d2dd8579f5fa29c")
}
db.products.insertMany([
{name: "Product 2", price: 20, quantity: 200, category: "Category 2"},
{name: "Product 3", price: 30, quantity: 300, category: "Category 3"},
{name: "Product 4", price: 40, quantity: 400, category: "Category 4"}
])
o/p:
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("6257a2ad7d2dd8579f5fa29d"),
ObjectId("6257a2ad7d2dd8579f5fa29e"),
ObjectId("6257a2ad7d2dd8579f5fa29f")
]
}
db.products.find({})
o/p:
{ "_id" : ObjectId("6257a1e27d2dd8579f5fa29c"), "name" : "Product 1", "price" : 10, "quantity" : 100, "category" : "Category 1" }
{ "_id" : ObjectId("6257a2ad7d2dd8579f5fa29d"), "name" : "Product 2", "price" : 20, "quantity" : 200, "category" : "Category 2" }
{ "_id" : ObjectId("6257a2ad7d2dd8579f5fa29e"), "name" : "Product 3", "price" : 30, "quantity" : 300, "category" : "Category 3" }
{ "_id" : ObjectId("6257a2ad7d2dd8579f5fa29f"), "name" : "Product 4", "price" : 40, "quantity" : 400, "category" : "Category 4" }
db.products.find({}, {name: 1, category: 1, _id: 0})
o/p:
{ "name" : "Product 1", "category" : "Category 1" }
{ "name" : "Product 2", "category" : "Category 2" }
{ "name" : "Product 3", "category" : "Category 3" }
{ "name" : "Product 4", "category" : "Category 4" }
// Update the "quantity" of the product with name "product1" to 20
db.products.updateOne(
{ "name": "product1" },
{ $set: { "quantity": 20 } }
)
// Verify the update
db.products.find({ "name": "product1" })
o/p:
{ "_id" : ObjectId("615d7e205b28bf954e183d6a"), "name" : "product1", "price" : 10, "quantity" : 20, "category" : "category1" }
set 8:
Create a new database called blog and a collection called posts.
Insert a new document into the posts collection with the fields title, content, author, and created_at.
Query the posts collection and retrieve all documents.
Query the posts collection and retrieve only the title and created_at fields of all documents.
Update the document for a specific post by its title field to change its content.
Solution.
use blog
db.createCollection("posts")
db.posts.insertOne({
title: "My First Blog Post",
content: "Hello, world! This is my first blog post.",
author: "Jane Doe",
created_at: new Date()
})
db.posts.find()
db.posts.find({}, {title: 1, created_at: 1})
db.posts.updateOne(
{title: "My First Blog Post"},
{$set: {content: "Updated content for my first blog post."}}
)
//to verify the changes
db.posts.find()
Comments
Post a Comment