How to efficiently create indexes and query them in MongoDB views?

My question is about MongoDB view optimization. For example, I have such code mongodb.js · GitHub and when I’m running a db.b.find({ day: 'Monday' }).explain() I see that query is appended at the end of the view pipeline as $match stage operator. Is there any way to prepend this query to utilize indexes properly?

The view’s query is not using the index as the $match is coming in the later stages of the pipeline. The index can be used only when the $match is in the beginning stages (mostly first) of the pipeline.

To utilize the index, make the $match part of the view definition (then this query will use the underlying collection’s index). But, that limits the view’s capabilities (e.g., just query on one value, like “Monday” or a range of values).

As such there is no way to pass a ‘parameter’ value to a view’s definition; in this case the $match stage, if it is used as the view pipeline’s first stage. But, a JavaScript function with a query can take a parameter, and one can run the script from mongo Shell.