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
-
Make a custom
_id
fromMonthYear
since it is always unique? -
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?