Why is this code block is faster than previous one?

I added indexes for both franchiseProductId and storeId.

My previous code was like that and it was working with 3-4 seconds delay.

products.update({franchiseProductId: r.productId, storeId: product.storeId}, {$inc: {stockStore: -r.productAmount * amount}})

Than i changed it with this, it’s working without delay. i don’t know why.

let p = products.find({franchiseProductId: r.productId, storeId: product.storeId});
products.update(p._id, {$inc: {stockStore: -r.productAmount * amount}})

Can anybody explain?

Is it a compound index? If the individual values can repeat then compound index would be faster because of more distinct values. _id always has distinct values by definition, and the default primary key index is usually the fastest.

I added them like this,
products._ensureIndex({ storeId: 1, franchiseProductId:1 });

and franchiseProductId values are repeating.