0% found this document useful (0 votes)
5 views3 pages

MongoDB CheatSheet

SQL Vs Mongo DB Cheat Sheet

Uploaded by

abhishekkab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

MongoDB CheatSheet

SQL Vs Mongo DB Cheat Sheet

Uploaded by

abhishekkab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Queries:

SL Category SQL Mongo DB


1 SELECT SELECT * FROM users; db.users.find({})
2 WHERE SELECT * FROM users WHERE db.users.find({"age": 25})
age = 25;
3 AND SELECT * FROM users WHERE db.users.find({"age": 25, "city":
age = 25 AND city = 'New York'; "New York"})
4 OR SELECT * FROM users WHERE db.users.find({"$or": [{"age": 25},
age = 25 OR city = 'New York'; {"city": "New York"}]})
5 IN SELECT * FROM users WHERE db.users.find({"city": {"$in": ["New
city IN ('New York', 'Los York", "Los Angeles", "Chicago"]}})
Angeles', 'Chicago');
6 LIKE SELECT * FROM users WHERE db.users.find({"name": {"$regex":
name LIKE 'John%'; "^John"}})
7.1 UPDATE UPDATE users SET age = 30 db.users.update_one({"name":
WHERE name = 'John'; "John"}, {"$set": {"age": 30}})
7.2 db.users.update_many({"name":
"John"}, {"$set": {"age": 30}})
8.1 INSERT INSERT INTO users (name, age, db.users.insert_one({"name":
city) VALUES ('John', 25, 'New "John", "age": 25, "city": "New
York'); York"})
8.2 db.users.insert_many([
{"name": "Alice", "age": 28,
"city": "Chicago"},
{"name": "Bob", "age": 22,
"city": "Los Angeles"}
])
9.1 DELETE DELETE FROM users WHERE db.users.delete_one({"name":
name = 'John'; "John"})
9.2 db.users.delete_many({"name":
"John"})
10 LIMIT SELECT * FROM users LIMIT 5; db.users.find().limit(5)
11 ORDER BY SELECT * FROM users ORDER BY db.users.find().sort("age", -1) # -1
age DESC; for DESC, 1 for ASC
12 COUNT SELECT COUNT(*) FROM users; db.users.count_documents({})
13 SUM SELECT SUM(salary) FROM db.employees.aggregate([
employees; {"$group": {"_id": None,
"total_salary": {"$sum":
"$salary"}}}
])
14 INNER JOIN SELECT orders.order_id, db.orders.aggregate([
customers.name {
FROM orders "$lookup": {
JOIN customers ON "from": "customers", #
orders.customer_id = Collection to join with
customers.customer_id; "localField": "customer_id",
# Field in `orders`
"foreignField":
"customer_id", # Field in
`customers`
"as": "customer_details" #
Output field
}
}
])
15 LEFT JOIN SELECT orders.order_id, db.orders.aggregate([
customers.name {
FROM orders "$lookup": {
LEFT JOIN customers ON "from": "customers",
orders.customer_id = "localField": "customer_id",
customers.customer_id; "foreignField":
"customer_id",
"as": "customer_details"
}
}
])
16 RIGHT JOIN SELECT customers.name, db.customers.aggregate([
orders.order_id {
FROM customers "$lookup": {
RIGHT JOIN orders ON "from": "orders",
customers.customer_id = "localField": "customer_id",
orders.customer_id; "foreignField":
"customer_id",
"as": "order_details"
}
}
])
17 JOIN+FILTER SELECT orders.order_id, db.orders.aggregate([
customers.name {
FROM orders "$lookup": {
JOIN customers ON "from": "customers",
orders.customer_id = "localField": "customer_id",
customers.customer_id "foreignField":
WHERE customers.city = 'New "customer_id",
York'; "as": "customer_details"
}
},
{
"$match":
{"customer_details.city": "New
York"}
}
])
18 Single Index CREATE INDEX idx_name ON db.users.create_index("name")
users(name);
19 Compound CREATE INDEX idx_name_age db.users.create_index([("name",
Index ON users(name, age); 1), ("age", 1)]) # 1 for ascending, -
1 for descending
20 Unique CREATE UNIQUE INDEX db.users.create_index("email",
idx_email ON users(email); unique=True)
21 Text Index SELECT * FROM users WHERE db.users.create_index([("name",
name LIKE '%John%'; "text")])
db.users.find({"$text": {"$search":
"John"}})
22 TTL Index DELETE FROM logs WHERE db.logs.create_index("created_at",
created_at < NOW() - INTERVAL expireAfterSeconds=2592000) #
30 DAY; 30 days
23 Check Index SHOW INDEXES FROM users; db.users.index_information()
24 Drop Index DROP INDEX idx_name ON DROP INDEX idx_name ON users;
users;
25 Regex SELECT * FROM users WHERE db.users.find({"name": {"$regex":
name LIKE '%John%'; "John"}})
26 Starts With SELECT * FROM users WHERE db.users.find({"name": {"$regex":
name LIKE 'John%'; "^John"}})
27 Ends With SELECT * FROM users WHERE db.users.find({"name": {"$regex":
name LIKE '%John'; "John$"}})
28 Case SELECT * FROM users WHERE db.users.find({"name": {"$regex":
Insensitive name ILIKE '%john%'; "john", "$options": "i"}})
29

You might also like