1. What is real-time data analysis?
Real-time data analysis in MongoDB means collecting, processing, and analyzing data as soon as it is inserted or updated in the database, so that users can see the latest information with minimal delay.
MongoDB can analyze data using Aggregation Pipeline, detect database changes using Change Streams, and process continuous data streams using tools such as MongoDB Atlas Stream Processing.
Real-life examples
1. Online shopping system
Analyze orders, total sales, popular products, and revenue as new orders arrive.
2. IoT monitoring
Monitor temperature, humidity, and sensor readings to detect abnormal conditions.
3. Website analytics
Track active users, page views, and visitor activity as events are recorded.
2. How does it work?
1. Data is inserted into MongoDB
Example: A customer places an order
2. MongoDB processes the data
Aggregation Pipeline or Change Streams
3. Results are calculated
Total sales, order count, or alerts
4. Results are displayed
Dashboard, report, or notification
3. Example: Real-time sales analysis in MongoDB
Suppose we have an online shopping system. Whenever a customer places an order, we want to calculate the total sales for each product.
Step 1: Create a database and collection
Run these commands in MongoDB Shell (mongosh).
use shop
Insert sample order data:
db.orders.insertMany([
{
customer: "Amit",
product: "Laptop",
quantity: 2,
price: 50000
},
{
customer: "Riya",
product: "Mobile",
quantity: 3,
price: 15000
},
{
customer: "Raj",
product: "Laptop",
quantity: 1,
price: 50000
}
])
Step 2: Analyze the sales data
Use the Aggregation Pipeline to calculate the total sales for each product.
db.orders.aggregate([
{
$group: {
_id: "$product",
totalQuantity: { $sum: "$quantity" },
totalSales: {
$sum: {
$multiply: ["$quantity", "$price"]
}
}
}
}
])
Output:
| Product | Total quantity | Total sales |
|---|---|---|
| Laptop | 3 | 150000 |
| Mobile | 3 | 45000 |
The $group stage groups orders by product, $sum adds quantities and sales, and $multiply calculates each order’s value.
Step 3: Insert a new order
db.orders.insertOne({
customer: "Neha",
product: "Laptop",
quantity: 2,
price: 50000
})
Run the aggregation query again. The Laptop total will now be:
- Total quantity: 5
- Total sales: ₹250,000
This is near-real-time analysis when the query is rerun. To detect new orders automatically, use Change Streams.
4. Automatic real-time updates using Change Streams
MongoDB Change Streams allow an application to listen for database changes, such as inserted, updated, or deleted documents.
For example, run this in mongosh:
use shop
const stream = db.orders.watch();
stream.on("change", (change) => {
print("Database changed:");
printjson(change);
});
Keep this shell running. Open another MongoDB Shell and insert a new order:
use shop
db.orders.insertOne({
customer: "Kiran",
product: "Mobile",
quantity: 1,
price: 15000
})
The first shell will automatically display a change event containing information about the inserted document.
Important: Change Streams require a replica set or sharded cluster. They do not work on a standalone MongoDB server. If you are using a standalone installation, configure a replica set first.
5. Important MongoDB features for real-time analysis
| Feature | Purpose |
|---|---|
| Aggregation Pipeline | Summarizes and analyzes data |
| Change Streams | Detects database changes automatically |
| Time Series Collections | Stores time-stamped sensor or monitoring data efficiently |
| Atlas Stream Processing | Processes continuous streams of incoming data |
| Indexes | Improve the speed of queries and analysis |
6. Aggregation vs. Change Streams
| Aggregation Pipeline | Change Streams |
|---|---|
| Calculates results from stored data | Detects changes as they occur |
| Example: Calculate total sales | Example: Detect a new order |
| Runs when a query is executed | Continuously listens for changes |
| Can run on a standalone MongoDB server | Requires a replica set or sharded cluster |
Conclusion: MongoDB supports real-time data analysis by combining aggregation for calculations with Change Streams for automatic change detection. An application can use both to update a sales dashboard or trigger alerts whenever new data arrives.