运行时错误:MongoDB :: DatabaseError:错误提示

时间:2017-07-15 18:43:58

标签: mongodb perl

我打算拨打hint method on a MongoDB::Cursor object。但是,它在尝试执行查询时抛出异常。请参阅下面的代码示例:

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;
}

对于发生了什么以及如何解决这个问题的想法?

1 个答案:

答案 0 :(得分:1)

Harish通过电子邮件问我,我会在这里重复我的回答,为后代:

hint方法在给定索引名称时采用字符串,或在给定键/订单对时采用数组引用

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

它也需要一个哈希引用,但是不要使用它,因为现代Perls在序列化时随机化了键顺序,所以你的提示可能与索引不匹配。

相关问题