Harish Harish - 2 months ago 11
Perl Question

Runtime error: MongoDB::DatabaseError: bad hint

I am trying to call the

method on a
MongoDB::Cursor
object
. However, it throwing an exception when it's trying to execute the query. See the code sample below:

sub some_method_which_returns_cursor {

my $cursor = $collection->find($filter);

if ($hint) {
$cursor->hint({‘some_index’ => 1}); #failing here.
}

if ($sort) {
$cursor->sort($sort);
}

return $cursor;
}


Any thoughts as to what's going on and how I can fix this?

xdg xdg
Answer Source

Harish asked me via email and I'll repeat my answer here for posterity:

The hint method takes a string when given an index name, or an array reference when given keys/order pairs:

$cursor->hint("some_index");                # by name
$cursor->hint([field1 => 1, field2 => -1]); # by keys

It also takes a hash reference, but don't use that because modern Perls randomize key order when serializing, so your hint may not match an index.