5

I am migrating from the mongodb csharp driver 1.10.0 to 2.0.0. One of the collection I am using is very big and has to fulfill many queries with different filter attributes. That is why I was relying on some index hint statements. With the v1.10 driver it looks like

 myCollection.Find(query).SetHint("myIndexName");

I searched the v2 driver api but this hint method seems to be completly removed in the v2 driver. Is there an alternative? How should I do index hints with the v2 driver?

Note: The Solutions provided works for latest mongodb csharp drivers as well

4 Answers 4

9

You can use the FindOptions.Modifiers property.

var modifiers = new BsonDocument("$hint", "myIndexName"); 
await myCollection.Find(query, new FindOptions { Modifiers = modifiers }).ToListAsync();

May I ask why you are using the hint? Was the server consistently choosing the wrong index? You shouldn't need to do this except in exceptional cases.

Craig

Sign up to request clarification or add additional context in comments.

3 Comments

thank you! this worked. yes, we were experiencing that the execution plan was not optimal (can't remember if consistently or only sometimes) that led to a bad response time. we also had a mongodb consultant here. (mongodb 2.6 on windows)
For me, MongoDB 3.2 doesn't select the correct index when sorting on a field. There basically only is one specialized index, but explain doesn't even mention it. Hinting does the trick.
Upvoted, but just be really careful with this. Mongo is usually pretty intelligent in which index it uses
2

With agregate you can force indice like this:

BsonString bsonString = new BsonString("ix_indice");

var query = this.collection.Aggregate(new AggregateOptions() { Hint = bsonString }).Match(new BsonDocument {..});

Comments

1

Ideally, try to make the query in a way that mongodb optimizer can use the index automatically.

If you are using FindAsync then you will have a property named Hint. Use it like this:

If you have index named "myIndexName" which you want your query should use forcefully, then use like this:.

BsonString bsonString = new BsonString("myIndexName");

cursor = await collection.FindAsync(y => y.Population > 400000000,
new FindOptions<Person, Person>()
{
     BatchSize = 200,
     NoCursorTimeout = true,
     AllowPartialResults = true,
     Projection = "{'_id':1,'Name':1,'Population':1}"
     Hint = bsonString.AsBsonValue,
}).ConfigureAwait(false);

You can fine BsonString class in MongoDB.Bson

Comments

0

If you are using the Linq IQueryable, you can specify the hint (and other options) like this:

BsonDocument hint = new BsonDocument("myFieldName", 1);
// or
BsonString hint = new BsonString("myIndexName");

await collection.AsQueryable(new AggregateOptions { Hint = hint })

myFieldName can also reference a complex field, e.g. Metadata.FileName

myIndexName is the name of an index. I prefer to reference the field (first option) directly, instead of an index, for simple cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.