Avoid searching all documents when query field is unique?

I’m new to mongoDB and I’m trying to understand how to fetch data from the database

I have a collection containing month wise documents, an example of the collection would be:

{
  MonthYear: "05-2023",
  ...
}

{
  MonthYear: "06-2023",
  ...
}

{
  MonthYear: "07-2023",
  ...
}

When I search a collection as such:

db.collection.find({"MonthYear": "06-2023"}).explain("executionStats")
"executionStats": {
    "executionStages": {
      ...
      "docsExamined": 3,
    }
...
}

It searches through all the available documents (which may be a huge quantity) till it finds the date, which seems like a waste when the MonthYear is always unique and hence should be known exactly where to look i.e. with no looping search

So my question is should I

  1. Make a custom _id from MonthYear since it is always unique?

  2. or should I index MonthYear and if I should, will it lead to any performance loss?

Or should I be taking a completely different approach?

Searching on a field that is not indexed will always be slow, rather then overwrite the default document IDs I’d just add an index to the field you want to to search on.
Yes, it will add a slight overhead as it maintains the index on new inserts etc, but index update should be pretty fast so not add that much overhead to insert or updates.

Worst case try it out with your workload, insert 100K documents and check timings, create the index and do the same.
You could also profile searches with and without the index but it’s pretty pointless as it’ll be so much faster with it!

2 Likes

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