Can a query/aggregation with an $or operator scan multiple indexes?

Hi all.

If I had the following sample data:

name: “John”,
city: “London”
name: “Jason”,
city: “London”

And ran the following query:

db.collection.find({ $or: [ { name: “John” }, { city: “London” } ] })

Would the $or operator be able to scan both of the following indexes or would it just pick one, best-matching index?

{ name: 1 }
{ city: 1 }

Would I be better off using

{ city: 1, name: 1 }

For maximum efficiency here?

Thanks in advance.

Hey @Lewis_Dale,

Does the index usage information listed on the $or operator documentation help answer your questions?

Hope this helps.


Yes it does, I must have missed that. Thanks!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.