1
in the createIndex()
ascending order, while -1
method would specifies specify descending order.db.emp.createIndex({ "empid": 1 })
db.emp.getIndexes()
db.emp.dropIndexes("empid_1")
db.emp.createIndex({ "deptname": 1, "salary": 1 })
{
"_id": 1,
"book": "The Courageous Journey",
"authors": ["Rajesh Kumar", "Priyanka Sharma"]
}
db.books.createIndex({authors:1})
{
"_id": ObjectId,
"name": String,
"description": String,
"category": String,
"price": Number,
"tags": [String]
}
db.products.createIndex({ "$**": "text" })
2dsphere
index (which allows you to perform geospatial queries like finding nearby locationsdb.places.insertMany([
{ name: "Cafe A", location: { type: "Point", coordinates: [40.7128, -74.0060] } }, // New York City
{ name: "Cafe B", location: { type: "Point", coordinates: [34.0522, -118.2437] } }, // Los Angeles
{ name: "Cafe C", location: { type: "Point", coordinates: [51.5074, -0.1278] } } // London
])
db.places.createIndex({ location: "2dsphere" })
db.places.find({
location: {
$nearSphere: {
$geometry: { type: "Point", coordinates: [40.7128,-74.0060] },
$maxDistance: 1000000 // In meters, e.g., 1,000 km radius
}
}
})
db.emp.createIndex({ "empid": 1 })
Craft Efficient Filters:
$eq
(equality), $gt
, $lt
(range comparisons), and $in
to narrow down the search results.$or
and $not
operators can degrade performance.Optimize Projections:
db.emp.find({}, {eno:1,ename:1})
limit()
to restrict the number of documents returned.db.emp.find().limit(3)
Utilize the Explain Plan:
explain()
output to pinpoint areas for improvement in your queries and indexes.explain()
method provides detailed information about how MongoDB executes a query, including index usage, scan counts, and execution time.explain()
method to analyze query execution plans, identify bottlenecks, and optimize query performance.Consider Aggregation Framework:
db.emp.aggregate([{
$group: {
_id: "$deptname",
totalEmployees: { $sum: 1 }
}
}])
Sharding for Large Datasets:
Caching:
Data Modelling:
Continuous Monitoring and Refinement:
Sharding helps you manage very large datasets that won't fit on a single machine.
Sharding is used when your data becomes too large to fit on a single server.
Instead of storing all the data on one machine, sharding divides the data into chunks and spreads those chunks across multiple machines (called shards)
By spreading the data across multiple machines, MongoDB can process queries faster, as it only needs to search through a portion of the data on each machine.
It's like splitting up a huge file into smaller pieces and storing those pieces on different computers so that each computer can work on a part of the file.
Sharding allows you to add more machines (shards) as your data grows, helping the database handle more traffic.
Example:
order_id
(shard key).order_ids
.When to use?
Split: Split key range into tow key ranges when size of chunk is too big
Migrate: To maintain balance migrate chunk from one shard to another.
Selection of Shard Key:
Characteristics of a Good Shard Key:
Single Field Shard Key: Use when one field is frequently used in queries.
{
"empid": 101,
"Joindate": "2024-01-15",
"salary": 50000
}
{
" id": 1,
"customer. id": 101,
"2e24-ø1-15",
"amount": 500,
"status": "shipped,
}
mongods
where metadata of clusters are stored.mongos
: clients connect to view whole cluster as single entity.mongod
: Process for data store.Aspect | Replica Set | Sharding |
---|---|---|
Purpose | Makes copies of data to keep it safe and available. | Helps to spread data across many computers. |
How it Works | Makes copies of the same data on different computers. | Splits data into smaller parts and stores them on different computers |
When to Use | When you want to make sure your data is always safe, even if one computer breaks. | When you have a lot of data that’s too big for one computer. |
Good for | Keeping data safe, so if one computer breaks, you can still get your data | Handling huge amounts of data and keeping everything fast. |
Challenges | Needs extra space because it keeps copies of the data | Can be tricky to set up, especially deciding how to split the data. |
Example | Keeping copies of important school data in case your main computer crashes. | A big online store that needs to split customer orders across many computers. |
explain()
in query optimization? Demonstrate its usage with an example.Made By SOU Student for SOU Students