sample questions 3
set1
- Find all documents in a collection named "books".
- Find all documents in a collection named "customers" where the city is equal to "New York".
- Find a document in a collection named "users" where the username is equal to "johndoe"
- Insert a new document into a collection named "products" with the following fields: name, category, price, and quantity.
- Update the price of a product in a collection named "products" where the product ID is equal to 1234.
- Delete a document from a collection named "orders" where the order ID is equal to 5678.
- Write a query to find all banks that are located in California.
- Write a query to find the bank with the highest number of employees.
- Write a query to update the "total_assets" field of the bank with the _id of 1 to 1200000000.
- Write a query to find all projects that are not started.
- Write a MongoDB query to find all the projects that have a status of "completed" and a budget greater than $100,000.
- Write a MongoDB query to find the project with the highest value in the "budjet" field.
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