sample questions 1
Set1.
•
Create a new database in MongoDB using the use command. Insert a
document with your name, email address, and phone number into a new collection
called contacts.
•
Query the contacts collection and retrieve all documents using
the find method.
•
Query the contacts collection and retrieve only the name field
of all documents using the find method and projection.
• Insert two new
documents into the contacts collection. One should have the name "John
Smith" and the other should have the name "Jane Doe".
Answer:
use
myDatabase
o/p: switched to db myDatabase
db.contacts.insertOne({
name: "John Doe",
email: "johndoe@example.com",
phone: "123-456-7890"
})
o/p:
{ "acknowledged" : true, "insertedId" : ObjectId("...") }
db.contacts.find()
{ "_id" : ObjectId("..."), "name" : "John Doe", "email" : "johndoe@example.com", "phone" : "123-456-7890" }
db.contacts.find({},
{name: 1})
{ "_id" : ObjectId("..."), "name" : "John Doe" }
db.contacts.insertMany([
{
name: "John Smith",
email: "johnsmith@example.com",
phone: "987-654-3210"
},
{
name: "Jane Doe",
email: "janedoe@example.com",
phone: "555-555-5555"
}
])
{ "acknowledged" : true, "insertedIds" : [ ObjectId("..."), ObjectId("...") ] }
Set2.
•
Create a new database in MongoDB using the use command. Insert a document with
your name, email address, and phone number into a new collection called
contacts.
•
Insert two new documents into the contacts collection. One should have the name
"John Smith" and the other should have the name "Jane Doe".
•
Update the document for "John Smith" to include an additional field
called address with the value "123 Main St".
•
Update the document for "John Doe" to include an additional field
called address with the value "125 Second St".
Solution
use
myDatabase
db.contacts.insertOne({
name: "John Doe",
email: "johndoe@example.com",
phone: "123-456-7890"
})
switched to db myNewDatabase
{
"acknowledged" : true,
"insertedId" : ObjectId("...")
}
db.contacts.insertMany([
{
name: "John Smith",
email: "johnsmith@example.com",
phone: "987-654-3210"
},
{
name: "Jane Doe",
email: "janedoe@example.com",
phone: "555-555-5555"
}
])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("..."),
ObjectId("...")
]
}
db.contacts.updateOne(
{name: "John Smith"},
{$set: {address: "123 Main St"}}
)
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
db.contacts.updateOne(
{name: "John Doe"},
{$set: {address: "125 Second St"}}
)
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
Set3.
. insert 5 documents
in contacts collection
{
"name":
"John Smith",
"email":
"john.smith@example.com",
"phone":
"+1 (555) 123-4567",
"address":
{
"street": "123 Main St",
"city":
"Anytown",
"state":
"CA",
"zip":
"12345",
"country": "USA"
}
},
{
"name":
"Jane Doe",
"email":
"jane.doe@example.com",
"phone":
"+1 (555) 987-6543",
"address":
{
"street": "456 Oak St",
"city":
"Sometown",
"state":
"NY",
"zip":
"54321",
"country": "USA"
}
},
{
"name":
"Bob Johnson",
"email":
"bob.johnson@example.com",
"phone":
"+1 (555) 555-5555",
"address":
{
"street": "789 Elm St",
"city":
"Anyville",
"state":
"TX",
"zip":
"67890",
"country": "USA"
}
},
{
"name":
"Emma Williams",
"email":
"emma.williams@example.com",
"phone":
"+44 123 456 7890",
"address":
{
"street": "10 Downing St",
"city":
"London",
"state":
"",
"zip":
"SW1A 2AA",
"country": "UK"
}
},
{
"name":
"Hiroshi Nakamura",
"email":
"hiroshi.nakamura@example.com",
"phone":
"+81 3-1234-5678",
"address":
{
"street": "2-1-2 Otemachi",
"city":
"Chiyoda-ku",
"state":
"Tokyo",
"zip":
"100-0004",
"country": "Japan"
}
}
Query the contacts collection and retrieve all documents
sorted by name in ascending order.
db.contacts.find({}).sort({name:1})
{ "_id" : ObjectId("609db534e5d5a5c5d5b5bf5c"), "name" : "Bob Johnson", "email" : "bob.johnson@example.com", "phone" : "+1 (555) 555-5555", "address" : { "street" : "789 Elm St", "city" : "Anyville", "state" : "TX", "zip" : "67890", "country" : "USA" } }
{ "_id" : ObjectId("609db534e5d5a5c5d5b5bf5b"), "name" : "Emma Williams", "email" : "emma.williams@example.com", "phone" : "+44 123 456 7890", "address" : { "street" : "10 Downing St", "city" : "London", "state" : "", "zip" : "SW1A 2AA", "country" : "UK" } }
{ "_id" : ObjectId("609db534e5d5a5c5d5b5bf5d"), "name" : "Hiroshi Nakamura", "email" : "hiroshi.nakamura@example.com", "phone" : "+81 3-1234-5678", "address" : { "street" : "2-1-2 Otemachi", "city" : "Chiyoda-ku", "state" : "Tokyo", "zip" : "100-0004", "country" : "Japan" } }
{ "_id" : ObjectId("609db534e5d5a5c5d5b5bf5a"), "name" : "Jane Doe", "email" : "jane.doe@example.com", "phone" : "+1 (555) 987-6543", "address" : { "street" : "456 Oak St", "city" : "Sometown", "state" : "NY", "zip" : "54321", "country" : "USA" } }
{ "_id" : ObjectId("609db534e5d5a5c5d5b5bf59"), "name" : "John Doe", "email" : "johndoe@example.com", "phone" : "123-456-7890", "address" : "125 Second St" }
{ "_id" : ObjectId("609db534e5d5a5c5d5b5bf58"), "name" : "John Smith", "email" : "john.smith@example.com", "phone" : "+1 (555) 123-4567", "address" : { "street" : "123 Main St", "city" : "Anytown", "state" : "CA", "zip" : "12345", "country" : "USA" } }
Query the contacts collection and retrieve all documents
sorted by name in descending order.
db.contacts.find({}).sort({name:-1})
4.
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
switched to db blog
> db.createCollection("posts")
{ "ok" : 1 }
> db.posts.insertOne({
... title: "My First Blog Post",
... content: "Hello, world! This is my first blog post.",
... author: "Jane Doe",
... created_at: new Date()
... })
{
"acknowledged" : true,
"insertedId" : ObjectId("...") // ObjectId will be a unique ID generated by MongoDB
}
> db.posts.find()
{ "_id" : ObjectId("..."), "title" : "My First Blog Post", "content" : "Hello, world! This is my first blog post.", "author" : "Jane Doe", "created_at" : ISODate("2023-04-17T20:52:18.854Z") }
> db.posts.find({}, {title: 1, created_at: 1})
{ "_id" : ObjectId("..."), "title" : "My First Blog Post", "created_at" : ISODate("2023-04-17T20:52:18.854Z") }
> db.posts.updateOne(
... {title: "My First Blog Post"},
... {$set: {content: "Updated content for my first blog post."}}
... )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
5.
1. Create a new database called ecommerce and a collection
called products.
2. Insert 5 new documents into the products collection, each
with the fields name, description, price, quantity, and created_at.
3. Query the products collection and retrieve all documents.
4. Query the products collection and retrieve only the name and
price fields of all documents.
5. Update the document for a specific product by its name field
to change its quantity
Solution
switched to db ecommerce
> db.createCollection("products")
{ "ok" : 1 }
> db.products.insertMany([
... {
... name: "Product A",
... description: "This is product A",
... price: 10.99,
... quantity: 50,
... created_at: new Date()
... },
... {
... name: "Product B",
... description: "This is product B",
... price: 19.99,
... quantity: 20,
... created_at: new Date()
... },
... {
... name: "Product C",
... description: "This is product C",
... price: 15.99,
... quantity: 30,
... created_at: new Date()
... },
... {
... name: "Product D",
... description: "This is product D",
... price: 29.99,
... quantity: 10,
... created_at: new Date()
... },
... {
... name: "Product E",
... description: "This is product E",
... price: 9.99,
... quantity: 100,
... created_at: new Date()
... }
... ])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("61717a6f20b6c1c7d50b0e01"),
ObjectId("61717a6f20b6c1c7d50b0e02"),
ObjectId("61717a6f20b6c1c7d50b0e03"),
ObjectId("61717a6f20b6c1c7d50b0e04"),
ObjectId("61717a6f20b6c1c7d50b0e05")
]
}
> db.products.find()
{ "_id" : ObjectId("61717a6f20b6c1c7d50b0e01"), "name" : "Product A", "description" : "This is product A", "price" : 10.99, "quantity" : 50, "created_at" : ISODate("2023-04-17T10:25:19.973Z") }
{ "_id" : ObjectId("61717a6f20b6c1c7d50b0e02"), "name" : "Product B", "description" : "This is product B", "price" : 19.99, "quantity" : 20, "created_at" : ISODate("2023-04-17T10:25:19.973Z") }
{ "_id" : ObjectId("61717a6f20b6c1c7d50b0e03"), "name" : "Product C", "description" : "This is product C", "price" : 15.99, "quantity" : 30, "created_at" : ISODate("2023-04-17T10:25:19.973Z") }
{ "_id" : ObjectId("61717a6f20b6c1c7d50b0e04"), "name" : "Product D", "description" : "This is product D", "price" : 29.99, "quantity" : 10, "created_at" : ISODate("2023-04-17T10:25:19.973Z") }
db.products.find({}, {name: 1, price: 1})
o/p;
db.products.updateOne( {name: "Product A"}, {$set: {quantity: 100}}
o/p:
{ "acknowledged": true, "matchedCount": 1, "modifiedCount": 1 }
6.
1. Create a new database called music and a collection called indian_artists.
2. Insert 3 new documents into the artists collection, each with
the fields name, genre, albums, and members.
3. Query the artists collection and retrieve all documents.
4. Query the artists collection and retrieve only the name and
genre fields of all documents. 5. Update the document for a specific artist by
its name field to change its genre.
db.createCollection("indian_artists")
7.
Write a Python program
in Jupyter to create following documents in MongoDB
DB: Fruits
Collection: New
Documents:
{ name: banana,
color:yellow, no:5, price:10}
{ name: grape,
color:dark blue, no:100, price:2}
DB: Countries
Collection: New1
Documents:
{ name: India,
capital:delhi, no_states:28 }
{ name: USA,
capital:Washington DC, no_states:50}
Solution:
pip install pymongo
import pymongo
# Connect to MongoDB
server
client =
pymongo.MongoClient("mongodb://localhost:27017/")
# Create the Fruits
database and New collection
db =
client["Fruits"]
collection =
db["New"]
# Define the documents
to insert
document1 = {
"name": "banana",
"color": "yellow",
"no": 5,
"price": 10
}
document2 = {
"name": "grape",
"color": "dark blue",
"no": 100,
"price": 2
}
# Insert the documents
into the collection
collection.insert_many([document1,
document2])
# Create the Countries
database and New1 collection
db =
client["Countries"]
collection =
db["New1"]
# Define the documents
to insert
document1 = {
"name": "India",
"capital": "delhi",
"no_states": 28
}
document2 = {
"name": "USA",
"capital": "Washington
DC",
"no_states": 50
}
# Insert the documents
into the collection
collection.insert_many([document1,
document2])
8. Write a Python
program in Jupyter to create following documents in MongoDB
DB: Sports
Collection: New1
Documents:
{ name: cricket,
level:international, no:11,
players:{batsmen:[ram,lokesh,veeru,bodhan,rajesh],bowlers:[venkat,kapil,gowtham],Allrounders:[jaiswal,raju]
WK:[kethan]}}
{ name: hockey,
level:national, no:10, players:{GK:1,Offense:4,defense:5}
Solution:
pip install pymongo
import pymongo
# Connect to MongoDB server
client = pymongo.MongoClient("mongodb://localhost:27017/")
# Create the Sports database and New1 collection
db = client["Sports"]
collection = db["New1"]
# Define the documents to insert
document1 = {
"name":
"cricket",
"level":
"international",
"no": 11,
"players": {
"batsmen":
["ram", "lokesh", "veeru", "bodhan",
"rajesh"],
"bowlers":
["venkat", "kapil", "gowtham"],
"Allrounders":
["jaiswal", "raju"],
"WK":
["kethan"]
}
}
document2 = {
"name":
"hockey",
"level":
"national",
"no": 10,
"players": {
"GK": 1,
"Offense": 4,
"defense": 5
}
}
# Insert the documents into the collection
collection.insert_many([document1, document2])
Comments
Post a Comment