Covered queries won’t work in multikey indexes when the fields to be covered are in an array, you can check this in this part of the MongoDB documentation.
“merchant.item” is an array of objects and has around 30 fields inside. Also, documents are in millions in the collection.
I am not quite sure if adding wildcard indexes will be performance efficient. What you suggest?
I would suggest try this option and see if the index being used or not . if does not help , you can drop… as per the documentation , this index option for array of objects only.
I executed the following code in the Mongosh, and it worked, the find operation used the index. You can try it by just copying and paste to your Mongosh.
Thanks @Kaushal_Kishor
This one worked. I still need to check further on performance and also how it will impact existing related indexes.
Will keep you posted.
I have a question.
What is the difference in wildcard indexes for “merchant.$” and “merchant.item.$”
During find, both indexes will look for path “merchant.item" and “merchant.item.name”
or
“merchant.item" and “merchant.item.sku”
So why we selected “merchant.$” and not this “merchant.item.$”
The improvement in performance will be significant when you have enough documents so it doesn’t fit on the server cache, for small data sets, the time taken to execute the query will be very close. To have a better idea of the performance of a query, you can take a look at this page of the documentation.