0

Can mongodb take advantage of an index partially? I'm not talking about using a "partial index" ( https://docs.mongodb.com/manual/core/index-partial/ ), but using a regular index partially.

For example, let's say I create this index that has two fields:

    eventLog = database.getCollection("event_log", EventItem.class);
    eventLog.createIndex(Indexes.ascending("userId", "eventDate"));

What happens if I try to find elements for a given userId ONLY e.g.:

find({userId:23423})

Will mongodb take advantage of the index that begins with the userId field? Or do I have to create another index to speed up the query, e.g.

    eventLog.createIndex(Indexes.ascending("userId"));
1

MongoDB can take advantage of a compound index when you query for the leading column(s), so in your case the index on userId and eventDate can be used for a query on userId. You don't need a separate index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.