Can MongoDB use multiple indexes in one query?

I am facing the decision to either use one collection with embedding or use 2 collections.

If I use one collection, then I only need to perform 1 query. If I use 2 collections, then I need to perform two queries. However, performing 2 questions allows me to use 2 indexes.

In light of the ability to use 2 indexes, can I expect better read performances by using 2 collections instead of embedding?

@Big_Cat_Public_Safety_Act this will entirely depend on your data model and how much data you’re embedding vs. referencing. Performing a single operation (embedding) will require less disk/cache activity and fewer network roundtrips to retrieve the data.

If the data you’re referencing is potentially an unbounded array (ex: { orders: [ { order_id: 1, ... }, { order_id: 2, ... } .... { order_id: nnn, .... } ] }) it may make more sense to store that in a separate collection and only filter for the subset of data you need at any given time.

In light of the ability to use 2 indexes, can I expect better read performances by using 2 collections instead of embedding?

If you’re retrieving 100% of the data in a document 100% of the time, embedding will be a better choice. If you’re referencing a lot of data per document, 2 collections may be beneficial.

The beauty of MongoDB is you have both options at your disposal, however choosing the appropriate design will depend entirely on your application’s usage and access patterns.

1 Like