0

Is there any way to force a find() or aggregate() query to refer/see a particular existing index in MongoDB. I am asking about the the scenario when a collection has more than one compound indexes.

excoder
  • 23
  • 6

2 Answers2

0

Yes, $hint is there for that. As mentioned in the documentation, you can use it like that:

db.users.find().hint( { age: 1 } )

What you put in argument is the definition of the index, and not its name. This query would force the use of the index on the age field. I'm not sure though whether it works for aggreate() call as well or not.

Nicolas
  • 2,151
  • 1
  • 25
  • 29
0

Aggregation does not support $hint. There is a open item in MongoDB https://jira.mongodb.org/browse/SERVER-7944

Rohit Jain
  • 2,052
  • 17
  • 19