Mongodb Indexing
1. **Problem Statement:**
We are performing several MongoDB operations on a large collection named `bigdata`.
2. **Insert Documents:**
We insert 200,000 documents where each document has:
- `account_no`: auto-incrementing integer from 1 to 200,000
- `balance`: a random integer between 0 and 1,000,000 (simulating bank balances)
3. **Insert Code Explanation:**
The loop: $$ \text{for } i=1 \text{ to } 200000 $$
Inside the loop:
$$ \text{insert document } \{ \"account_no\": i, \"balance\": \text{Math.round(Math.random()*1000000)} \} $$
4. **Verify Insert:**
Count total documents:
$$ db.bigdata.count() = 200000 $$
5. **Create Index:**
Create index on `account_no` to speed up queries:
$$ db.bigdata.createIndex(\{ \"account_no\": 1 \}) $$
6. **Query and Explain:**
Query the document where $account\\_no=69271$:
$$ db.bigdata.find(\{\"account_no\":69271\}).explain(\"executionStats\").executionStats.executionTimeMillis $$
Record the milliseconds taken as $t_1$.
7. **Exercise 6 - Delete Index:**
Delete the index on `account_no`:
$$ db.bigdata.dropIndex(\{\"account_no\":1\}) $$
8. **Create Index on `balance`:**
Create new index:
$$ db.bigdata.createIndex(\{\"balance\":1\}) $$
9. **Query on `balance=10000`:**
Query and record time taken as $t_2$:
$$ db.bigdata.find(\{\"balance\":10000\}) $$
10. **Drop `balance` index:**
$$ db.bigdata.dropIndex(\{\"balance\":1\}) $$
11. **Query again `balance=10000`:**
Query and record time as $t_3$.
12. **Comparison and Learning:**
- Observe $t_1$ should be much less than query time without any index (from Exercise 3).
- Observe $t_2$ should be less than $t_3$ showing index on `balance` speeds up queries.
This exercise shows how creating indexes on fields enhances query speed, especially for large datasets.