Setting hint for CountDocuments function

231 views
Skip to first unread message

Vimal Patel

unread,
Nov 4, 2019, 9:59:20 PM11/4/19
to mongodb-go-driver
I am attempting to utilize the driver's CountDocuments method and had the following questions in order to use it correctly:
  1. Does the Hint variable need to be set in the CountOptions struct passed in as the opts parameter when calling the CountDocuments method in order to improve performance? I came across a question on Stack Overflow and wanted to confirm whether the information provided in this answer https://stackoverflow.com/a/56720193 is correct and an index does not need to actually be specified as the hint for query to be optimized performance-wise.
  2. If the Hint variable does need to be set, what should the value of that variable be set to? Should the value of the variable be set to the key of the index to use, or should it be the list of fields that compose the key of the index to use?
Message has been deleted

Divjot Arora

unread,
Nov 14, 2019, 10:48:59 AM11/14/19
to mongodb-go-driver
Hi,

CountDocuments does not require a hint, but one can be provided as an option. The StackOverflow post you linked is related to ticket https://jira.mongodb.org/browse/GODRIVER-1156, which was closed due to inactivity. We were unable to reproduce any errors when using the SetHint option (I tested this on version 1.0.1 as was reported by the original user and also 1.1.3, which is the latest driver version).

If you want to provide a hint, it can either be the name of an index or a document describing the index specification. For example, a hint on an index for fields (name, age) could be given as "name_1_age_1" or bson.D{{"name", 1}, {"age", 1}}. This is assuming a custom name wasn't provided when creating the index so the default naming convention was used.

Vimal Patel

unread,
Nov 14, 2019, 11:17:41 AM11/14/19
to mongodb-go-driver
Divjot,

Thank you!

- Vimal Patel
Reply all
Reply to author
Forward
0 new messages