Everything so far was fast because the collection was tiny. Real catalogs are not tiny, so let's make it big.
Step 3.1: Load 200,000 products (about 30 minutes)
This script builds products from small word lists, using a simple number generator with a fixed starting point so that every run produces the same data. It inserts them in batches of 10,000, with ordered: false, which lets MongoDB keep going if one document in a batch fails. The documents have to pass the validator we wrote in Phase 1, which is why every price and stock level is a whole number.
const words = {
shoes : {
adj : ['Trail' , 'Court' , 'Road' , 'Hiking' , 'Studio' ],
noun : ['runner' , 'sneaker' , 'boot' , 'sandal' , 'loafer' ],
},
laptops : {
adj : ['Ultra' , 'Gaming' , 'Budget' , 'Pro' , 'Mini' ],
noun : ['laptop' , 'notebook' , 'ultrabook' , 'workstation' , 'chromebook' ],
},
books : {
adj : ['Practical' , 'Complete' , 'Short' , 'Visual' , 'Advanced' ],
noun : ['guide' , 'handbook' , 'primer' , 'cookbook' , 'atlas' ],
},
kitchen : {
adj : ['Cast iron' , 'Steel' , 'Ceramic' , 'Copper' , 'Bamboo' ],
noun : ['pan' , 'knife' , 'kettle' , 'grinder' , 'board' ],
},
};
const categories = Object .keys (words);
const tagList = ['sale' , 'new' , 'winter' , 'gift' , 'eco' ];
const startOfYear = Date .UTC (2025 , 0 , 1 );
let seed = 42 ;
const next = ( ) => (seed = (seed * 48271 ) % 2147483647 );
const pick = (list ) => list[next () % list.length ];
const between = (low, high ) => low + (next () % (high - low + 1 ));
for (let batchNo = 0 ; batchNo < 20 ; batchNo++) {
const batch = [];
for (let i = 1 ; i <= 10000 ; i++) {
const n = batchNo * 10000 + i;
const category = pick (categories);
const adj = pick (words[category].adj );
const noun = pick (words[category].noun );
batch.push ({
sku : 'P-' + String (n).padStart (6 , '0' ),
name : adj + ' ' + noun,
description : 'A ' + adj.toLowerCase () + ' ' + noun + ' for everyday use.' ,
category,
price_cents : between (500 , 200000 ),
stock : between (0 , 200 ),
tags : [pick (tagList)],
created_at : new Date (startOfYear + between (0 , 364 ) * 86400000 ),
});
}
await db.products .insertMany (batch, { ordered : false });
}
console .log ('products:' , await db.products .countDocuments ());
It may take a minute or two. The count includes the products from earlier phases.
Step 3.2: Measure before you fix anything (about 30 minutes)
Never tune on a hunch. explain tells us how MongoDB ran a query. The part we care about is a few numbers: how many index entries it looked at (keysExamined), how many whole documents it read (docsExamined), how many it returned, and the steps it took (stages). This small helper boils the long answer down to those:
const plan = async (cursor ) => {
const explain = await cursor.explain ('executionStats' );
let node = explain.queryPlanner .winningPlan ;
node = node.queryPlan ?? node;
const stages = [];
while (node) {
stages.push (node.stage );
node = node.inputStage ;
}
const stats = explain.executionStats ;
return {
stages : stages.join (' > ' ),
returned : stats.nReturned ,
keysExamined : stats.totalKeysExamined ,
docsExamined : stats.totalDocsExamined ,
ms : stats.executionTimeMillis ,
};
};
await plan (
db.products
.find ({ category : 'laptops' , price_cents : { $lt : 20000 } })
.sort ({ price_cents : 1 })
.limit (10 ),
);
COLLSCAN means a collection scan : MongoDB read every document in the collection to find the few that match. Compare docsExamined with returned. Reading hundreds of thousands of documents to hand back ten is the cost of having no index that fits the question.
Step 3.3: One index, then a better one (about 40 minutes)
An index is a sorted shortcut, like the index of a book. Our query asks for one category, a price range and the cheapest first. A compound index on category and then price_cents is sorted by category, and inside each category by price, so it can answer all three parts:
db.products .createIndex ({ category : 1 , price_cents : 1 });
await plan (
db.products
.find ({ category : 'laptops' , price_cents : { $lt : 20000 } })
.sort ({ price_cents : 1 })
.limit (10 ),
);
IXSCAN means an index scan. Look at docsExamined now. MongoDB walked a short stretch of the index and fetched only the documents it needed.
COLLSCAN reads every document IXSCAN reads a short stretch docsExamined: all docsExamined: a few each square is a document AAAWgmp1bWIAAAAeanVtZGMycGEAEQAQgAAAqgA4m3EDYzJwYQAAABZcanVtYgAAAEdqdW1kYzJtYQARABCAAACqADibcQN1cm46YzJwYTpjMzM1MGUzOC03ZWQ2LTRiMmUtOWM1OS0xODYwMDhkNzE2MzIAAAADl2p1bWIAAAApanVtZGMyYXMAEQAQgAAAqgA4m3EDYzJwYS5hc3NlcnRpb25zAAAAALxqd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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPZYQJKH0QVyowMfwB6QS6iPofz2+zxo+7S3SK3QvOkH89S/UznxFCPwEpU4j6TG7V0612npD52zptga0KEB/dMQcz4= A collection scan reads everything. An index scan reads a short stretch.
An index helps only the questions it fits. Change the question: the same filter, but sort by stock, biggest first:
await plan (
db.products
.find ({ category : 'laptops' , price_cents : { $lt : 20000 } })
.sort ({ stock : -1 })
.limit (10 ),
);
If you see a SORT stage, MongoDB had to collect the matching documents and sort them in memory, because the index is not ordered by stock. A good rule for compound indexes is Equality, Sort, Range . Put fields you compare with equals first, then the field you sort by, then the fields you filter with a range. Our query has category as the equality, stock as the sort and price_cents as the range:
db.products .createIndex ({ category : 1 , stock : -1 , price_cents : 1 });
await plan (
db.products
.find ({ category : 'laptops' , price_cents : { $lt : 20000 } })
.sort ({ stock : -1 })
.limit (10 ),
);
The order of the fields in an index matters. The same three fields in a different order would serve a different set of questions.
Step 3.4: Search by words (about 30 minutes)
A shopper types "trail runner", not an exact category. A text index splits text fields into words, ignores letter case and common endings, and lets us search them. A collection can have only one text index, so we cover both fields at once and give a name match five times the weight of a description match:
db.products .createIndex (
{ name : 'text' , description : 'text' },
{ weights : { name : 5 , description : 1 }, name : 'product_text' },
);
db.products
.find (
{ $text : { $search : 'trail runner' } },
{ _id : 0 , sku : 1 , name : 1 , score : { $meta : 'textScore' } },
)
.sort ({ score : { $meta : 'textScore' } })
.limit (5 );
$text finds documents that contain any of the words, and the textScore we asked for ranks the ones that contain more of them, or contain them in the heavier field, first.
Step 3.5: What the indexes cost (about 20 minutes)
Indexes are not free. Each one takes disk space and memory, and every insert and update has to keep all of them up to date. Let's see the space:
const stats = await db.runCommand ({ collStats : 'products' });
const mb = (bytes ) => Math .round ((bytes / 1024 / 1024 ) * 10 ) / 10 ;
console .log ({
documents : stats.count ,
data_mb : mb (stats.size ),
index_mb : mb (stats.totalIndexSize ),
});
for (const [name, bytes] of Object .entries (stats.indexSizes )) {
console .log (name, mb (bytes) + ' MB' );
}
Compare data_mb with index_mb. The more indexes a collection has, the slower its writes and the more memory it wants. Keep the ones your real queries use, and drop the rest.
Phase 3 done
A collection of 200,000 products now answers the three questions it will be asked: filter and sort by price, filter and sort by stock, and search by words. You measured each change with explain instead of guessing. Next we turn orders into a report.