1 / 1
Mar 31

I created a test collection with the following documents:

db.products.insertMany([ { "name": "Smartphone A", "category": "electronics", "price": 4999, "attributes": { "brand": "Brand A", "color": "black", "storage": "128GB", "screen": { "size": 6.1, "type": "OLED" }, "camera": { "main": "48MP", "front": "12MP" }, "specifications": ["5G", "waterproof", "fast charging"] } }, { "name": "Smartphone B", "category": "electronics", "price": 6999, "attributes": { "brand": "Brand B", "color": "blue", "storage": "256GB", "screen": { "size": 6.7, "type": "AMOLED" }, "camera": { "main": "64MP", "front": "16MP", "features": ["night mode", "ultra-wide"] } } }, { "name": "Laptop X", "category": "electronics", "price": 8999, "attributes": { "brand": "Brand C", "color": "silver", "processor": "i7", "memory": "16GB", "storage": { "type": "SSD", "capacity": "512GB" }, "graphics": "dedicated GPU" } }, { "name": "Sport Shoes Alpha", "category": "clothing", "price": 899, "attributes": { "brand": "Brand D", "color": "white", "size": 42, "material": "mesh", "features": ["shock absorption", "lightweight"] } }, { "name": "Coffee Machine Pro", "category": "home appliance", "price": 2499, "attributes": { "brand": "Brand E", "color": "black", "type": "capsule", "functions": ["preheating", "auto-cleaning", "programmable"], "capacity": { "water": "1.5L", "beans": "200g" } } } ]);

Then I created a compound wildcard index:

db.products.createIndex({ "category": 1, "attributes.$**": 1 });

When I run this query:

db.products.find({ "category": "electronics", "attributes.color": "black", "attributes.brand": "Brand A" })

Why doesn’t it use the wildcard part of the index to filter on the attributes?

The execution plan shows the indexBounds as:

{ "category" : [ "[\"electronics\", \"electronics\"]" ], "$_path" : [ "[MinKey, MaxKey]" ] }

Instead of what I expected:

{ "category" : [ "[\"electronics\", \"electronics\"]" ], "$_path" : [ "[\"attributes.brand\", \"attributes.brand\"]" ], "attributes.brand" : [ "[\"Brand A\", \"Brand A\"]" ] }

Please let me know If I’m not clear enough with my question. Thank you