MONGO
DB
Q: To open all Databases
show dbs
Q: To switch to specific DB
use staffdb
Q: To drop a database
db.dropDatabase()
Q: To clear screen
cls
Q: To know which database we are working now
db
Q: To insert data in collection or db
db.users.insertone({name:”john”})
db.users.insertOne({name:"john"})
Q: To find inserted data in a collection
db.users.find()
Q: To insert data with complete address
db.users.insertone({name:”sally”, age:19, address:{street: “bezawada”},
hobbies:[“running”])
db.users.insertOne({name:"sally", age:19,
address:{street: "bezawada"}, hobbies:["running"]})
then enter this query
db.users.find()
Q: To show only 2 records from collection
db.users.find().limit(2)
db.users.find().limit(2)
Q: To find 1st user datails
db.users.find().sort({name: 1})
db.users.find().sort({name: 1})
Q: To skip 1st user details
db.users.find().skip(1)
Q: To find some one details with name
db.users.find({name: “john”})
Q: To find some one details with age
db.users.find({age: “19”})
GIT HUB projects
SCAN this QR code to connect with git hub projects
Q: To use not equals to
db.users.find({ name: { $ne:"john" }})
db.users.find({name: {$ne:”john”}})
Q: To find age greater than 13 years
db.users.find({age: {$gt:13}})
db.users.find({age: {$gt:13}})
Q: Q: To find age greater than or equals to 13 years
db.users.find({age: {$gte:13}})
db.users.find({age: {$gte:13}})
Q: Q: To find age less than 13 years
db.users.find({age: {$lt:13}})
db.users.find({age: {$lt:13}})
Q: Q: To find age less than or equals to 13 years
db.users.find({age: {$lte:13}})
db.users.find({age: {$lte:13}})
Q: To find multiple people from collection
db.users.find({name: {$in:["john","sally"]}})
db.users.find({name: {$in:[”john”,”sally”]}})
Q: To find multiple people not in collection or filter
db.users.find({name: {$nin:["john","sally"]}})
db.users.find({name: {$nin:[”john”,”sally”]}})
Q: To find the age data is there or not in collection
db.users.find({age: {$exists:true}})
db.users.find({age: {$exists:true}})
Q: To find the age data is there or not in collection
db.users.find({age: {$exists:false}})
db.users.find({age: {$exists:false}})
Q: To find the age between certain Numbers like 20 to 30
db.users.find({age:{$gte:10, $lte:20}})
db.users.find({age: {$gte:10, $lte:20}})
Q: To find the age between certain Numbers like 20 to 30
with a specific name
db.users.find({age:{$gte:10, $lte:20}, name:”sally”})
db.users.find({age:{$gte:10, $lte:20}, name:"sally"})
Q: To find a person with specific age and name exactly
db.users.find({$and:[{age:26},{name:’sally’}]})
db.users.find({$and:[{age: 26},{name:'sally'}]})
Q: To find a person with lessthan 20yrs age and name exactly
db.users.find({$or:[{age:{$lte:26}},{name:’sally’}]})
db.users.find({$or:[{age:{$lte:26}},{name:'sally'}]})
Q: To find age not lessthan or equals to 20
db.users.find({age:{$not:{$lte:20}}})
db.users.find({age:{$not:{$lte:20}}})
Q: To find age not greater than to 20
db.users.find({age:{$not:{$gt:20}}})
db.users.find({age:{$not:{$gt:20}}})
Q: To delete one user
db.users.deleteOne({name:"john"})
db.users.deleteOne({name:"john"})
https://www.youtube.com/watch?v=ofme2o29ngU
Project
Save this file as customers.json
[
{
"_id": 1,
"name": "John Doe",
"email": "johndoe@example.com",
"age": 30,
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
},
"orders": [
{ "order_id": 101, "amount": 250, "date": "2024-01-15" },
{ "order_id": 102, "amount": 150, "date": "2024-02-01" }
]
},
{
"_id": 2,
"name": "Alice Smith",
"email": "alice@example.com",
"age": 25,
"address": {
"street": "456 Oak St",
"city": "Los Angeles",
"state": "CA",
"zip": "90001"
},
"orders": [
{ "order_id": 103, "amount": 100, "date": "2024-02-10" }
]
},
{
"_id": 3,
"name": "Bob Johnson",
"email": "bob@example.com",
"age": 35,
"address": {
"street": "789 Pine St",
"city": "Chicago",
"state": "IL",
"zip": "60601"
},
"orders": []
}
]
Open mongoDB
Create new database and collection
Click on + symbol then add
Enter this commands in MongoDB shell
Cmd 1:
use db1
Cmd 2:
db.customers.find({})
Cmd 3:
db.customers.findOne({ "email": "johndoe@example.com" })
Cmd 4:
db.customers.find({ "age": { "$gt": 30 } })
Cmd 5:
db.customers.find({ "address.city": "New York" })
Cmd 6:
db.customers.find({ "orders": { "$ne": [] } })
Cmd 7:
db.customers.find({ "orders.amount": { "$gt": 200 } })
Cmd 8:
db.customers.find({ "orders.date": { "$gt": "2024-02-01" } })
Cmd 9:
db.customers.insertOne({
"_id": 4,
"name": "David Brown",
"email": "david@example.com",
"age": 28,
"address": {
"street": "100 Elm St",
"city": "Houston",
"state": "TX",
"zip": "77001"
},
"orders": []
})
Cmd 10:
db.customers.updateOne(
{ "name": "John Doe" },
{ "$set": { "age": 31 } }
)
Cmd 11:
db.customers.deleteOne({ "_id": 3 })
How to Connect with MongoDB using Python code
Enter this command in command prompt
Then save this code as mongodb_connect.py
from pymongo import MongoClient
# MongoDB connection URI (replace with your actual connection string)
MONGO_URI = "mongodb://localhost:27017" # Change if using MongoDB Atlas
# Connect to MongoDB
client = MongoClient(MONGO_URI)
# Select database
db = client["mydatabase"]
# Select collection
collection = db["users"]
# Insert a sample document
user_data = {"name": "John Doe", "email": "john@example.com"}
inserted_id = collection.insert_one(user_data).inserted_id
print(f"Inserted document with ID: {inserted_id}")
# Retrieve and print documents
for user in collection.find():
print(user)
# Close the connection
client.close()
Amazon orders queries
[
{
"order_id": "ORD123456",
"user_id": "USR987654",
"product_id": "PRD111",
"product_name": "Wireless Headphones",
"quantity": 1,
"price": 60,
"total_amount": 60,
"order_status": "Shipped",
"payment_status": "Paid",
"payment_method": "Credit Card",
"shipping_street": "123 Main St",
"shipping_city": "Los Angeles",
"shipping_state": "CA",
"shipping_zip": 90001,
"shipping_country": "USA",
"order_date": "2025-03-25T10:30:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
},
{
"order_id": "ORD123456",
"user_id": "USR987654",
"product_id": "PRD222",
"product_name": "Smartphone Case",
"quantity": 2,
"price": 10,
"total_amount": 20,
"order_status": "Shipped",
"payment_status": "Paid",
"payment_method": "Credit Card",
"shipping_street": "123 Main St",
"shipping_city": "Los Angeles",
"shipping_state": "CA",
"shipping_zip": 90001,
"shipping_country": "USA",
"order_date": "2025-03-25T10:30:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
},
{
"order_id": "ORD123457",
"user_id": "USR987655",
"product_id": "PRD333",
"product_name": "Laptop",
"quantity": 1,
"price": 799,
"total_amount": 799,
"order_status": "Processing",
"payment_status": "Pending",
"payment_method": "PayPal",
"shipping_street": "456 Elm St",
"shipping_city": "New York",
"shipping_state": "NY",
"shipping_zip": 10001,
"shipping_country": "USA",
"order_date": "2025-03-26T14:00:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
},
{
"order_id": "ORD123458",
"user_id": "USR987656",
"product_id": "PRD444",
"product_name": "Wireless Headphones",
"quantity": 2,
"price": 500,
"total_amount": 1000,
"order_status": "Shipped",
"payment_status": "Paid",
"payment_method": "PayPal",
"shipping_street": "456 Elm St",
"shipping_city": "Los Angeles",
"shipping_state": "CA",
"shipping_zip": 10001,
"shipping_country": "USA",
"order_date": "2025-03-25T10:30:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
},
{
"order_id": "ORD123459",
"user_id": "USR987657",
"product_id": "PRD555",
"product_name": "Smartphone Case",
"quantity": 1,
"price": 600,
"total_amount": 600,
"order_status": "Processing",
"payment_status": "Pending",
"payment_method": "PayPal",
"shipping_street": "123 Main St",
"shipping_city": "New York",
"shipping_state": "NY",
"shipping_zip": 90001,
"shipping_country": "USA",
"order_date": "2025-03-25T10:30:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
},
{
"order_id": "ORD123460",
"user_id": "USR987658",
"product_id": "PRD666",
"product_name": "Laptop",
"quantity": 2,
"price": 400,
"total_amount": 800,
"order_status": "Shipped",
"payment_status": "Paid",
"payment_method": "Credit Card",
"shipping_street": "456 Elm St",
"shipping_city": "Los Angeles",
"shipping_state": "NY",
"shipping_zip": 10001,
"shipping_country": "USA",
"order_date": "2025-03-26T14:00:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
},
{
"order_id": "ORD123461",
"user_id": "USR987659",
"product_id": "PRD777",
"product_name": "Wireless Headphones",
"quantity": 1,
"price": 500,
"total_amount": 500,
"order_status": "Processing",
"payment_status": "Pending",
"payment_method": "PayPal",
"shipping_street": "123 Main St",
"shipping_city": "New York",
"shipping_state": "NY",
"shipping_zip": 10001,
"shipping_country": "USA",
"order_date": "2025-03-25T10:30:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
},
{
"order_id": "ORD123462",
"user_id": "USR987660",
"product_id": "PRD888",
"product_name": "Smartphone Case",
"quantity": 2,
"price": 100,
"total_amount": 200,
"order_status": "Shipped",
"payment_status": "Paid",
"payment_method": "Credit Card",
"shipping_street": "456 Elm St",
"shipping_city": "New York",
"shipping_state": "NY",
"shipping_zip": 90001,
"shipping_country": "USA",
"order_date": "2025-03-26T14:00:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
},
{
"order_id": "ORD123463",
"user_id": "USR987661",
"product_id": "PRD999",
"product_name": "Laptop",
"quantity": 1,
"price": 600,
"total_amount": 600,
"order_status": "Shipped",
"payment_status": "Paid",
"payment_method": "Credit Card",
"shipping_street": "456 Elm St",
"shipping_city": "New York",
"shipping_state": "NY",
"shipping_zip": 10001,
"shipping_country": "USA",
"order_date": "2025-03-25T10:30:00Z",
"delivery_date": "2025-03-30T10:30:00Z"
}
]
1. Find all orders
2. Find orders by a specific user (USR987654)
3. Find all shipped orders
4. Find all orders with pending payment
5. Find all orders that were paid using PayPal
6. Get total sales amount for all orders
7. Find orders shipped to a specific city (Los Angeles)
8. Find orders shipped to a specific state (CA)
9. Find all orders for a specific product (PRD111 - Wireless Headphones)
11. Count the total number of orders
12. Find the highest order amount
13. Find the lowest order amount
14. Count the number of orders per payment method
15. Find orders that contain multiple products
16. Update order status to Delivered for a specific order (ORD123456)
17. Find orders where the quantity is greater than 1
18. Find total revenue generated by a specific product (PRD111)
19. Delete an order by Order ID (ORD123457)
20. Find the most popular product by total quantity sold
Fill application form and save data in MongoDB
Edit DB(Database), Collection Names.
from flask import Flask, render_template, request, redirect, url_for
from pymongo import MongoClient
app = Flask(__name__)
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["company"]
collection = db["dept"]
@app.route('/', methods=['GET', 'POST'])
def application_form():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
message = request.form['message']
# Save to MongoDB
application_data = {
"name": name,
"email": email,
"phone": phone,
"message": message
}
collection.insert_one(application_data)
return redirect(url_for('success'))
return '''
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.form-container {
background: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
input, textarea {
width: 100%;
padding: 10px;
margin: 5px 0;
border: 1px solid #ccc;
border-radius: 5px;
}
input[type="submit"] {
background-color: #28a745;
color: white;
border: none;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #218838;
}
</style>
</head>
<body>
<div class="form-container">
<form method="post">
<label for="name">Name:</label>
<input type="text" name="name" required><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br>
<label for="phone">Phone:</label>
<input type="text" name="phone" required><br>
<label for="message">Message:</label>
<textarea name="message" required></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
'''
@app.route('/success')
def success():
return "Application submitted successfully!"
if __name__ == '__main__':
app.run(debug=True)
The way you explain the complex topic that easily is truly amazing.
ReplyDeleteBig Data Solutions Company Chennai