[CSHARP-3273] $hint does not work with $natural as a modifier Created: 28/Nov/20  Updated: 27/Oct/23  Resolved: 01/Dec/20

Status: Closed
Project: C# Driver
Component/s: Read Operations
Affects Version/s: 2.11.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Caio César S. Leonardi Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

So, based on .hint() and $hint official documentation, I'm using this query on terminal:

 

db.Logs.find()._addSpecial( "$hint", { $natural : -1 } )

 

 

The closest I was able to produce on C# was something like:

 

 

var options = new FindOptions
            {
                Modifiers = new MongoDB.Bson.BsonDocument("$hint", "{ $natural : -1 }")
            };
 
//let me check the "translation"
var str = logCollection.Find(obj => true, options).ToString();
 
return logCollection.Find(obj => true, options).ToList();

 

This code throws the exception:

 

planner returned error :: caused by :: hint provided does not correspond to an existing index.'

 

But, the '.ToString()' method does seems to translate the command correctly:

 

find({ })._addSpecial("$hint", "{ $natural : -1 }")

 

So, if ToString() is getting it correctly, it should work, but it isn't.

 

On that note, shouldn't you create an C# equivalent to find().hint(index) to make it simpler to use hint ?

 

 



 Comments   
Comment by Robert Stam [ 01/Dec/20 ]

Thanks for submitting what looked like a possible bug.

Your code:

var options = new FindOptions
            {
                Modifiers = new BsonDocument("$hint", "{ $natural : -1 }")
            };

has a subtle error. 

The value of "$hint" should be a BsonDocument but you have set the value to a string.

The correct form would be:

var options = new FindOptions
            {
                Modifiers = new BsonDocument("$hint", new BsonDocument("$natural", -1))
            };

I'd also point out that the Modifiers option is Obsolete. You can provide a Hint more directly with:

var options = new FindOptions
            {
                Hint = new BsonDocument("$natural", -1)
            };

Generated at Wed Feb 07 21:44:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.