Documents in the collection follow the schema as given below -
{
"Year": String,
"Month": String
}
I want to accomplish the following tasks -
I want to run
distinct
queries likedb.col.distinct('Month', {Year: '2016'})
I have a compound index on
{'Year': 1, 'Month': 1}
, so by intuition looks like the answer can be computed by looking at indices only. The query performance on a collection in millions is really poor right now. Is there any way that this could be done?- I want to find the latest
month
of a givenyear
. One way is to sort the result of the abovedistinct
query and take the first element as the answer. A much better and faster solution as pointed out by @ Blakes Seven in the discussion below, would be to use the querydb.col.find({ "Year": "2016"}).sort({ "Year": -1, "Month": -1 }).limit(1)