@H_404_2@
我正在使用
official Perl driver与mongodb一起工作.要捕获和处理错误,Try :: Tiny和Safe :: Isa模块是
recommended.但是,它不能按预期工作.请检查下面的代码应该根据文档工作,但实际上它不起作用:
use MongoDB; use Try::Tiny; use Safe::Isa; my $client; try { $client = MongoDB->connect('mongodb://localhost'); $client->connect; } catch { warn "caught error: $_"; }; my $collection = $client->ns('foo.bar'); try { my $all = $collection->find; } catch { warn "2 - caught error: $_";; };
根据文档自动建立连接,connect()上不会有例外.但是请求也没有例外!我还将$client->连接字符串添加到force连接,但同样没有例外.我在没有安装mongodb并且没有运行mongodb docker容器的机器上运行此脚本,因此必须出现异常.
有人可以解释我做错了什么吗?
解决方法
这是一个微妙的问题. find返回游标对象,但不立即发出查询.从
MongoDB::Collection的文档:
Note,a MongoDB::Cursor object holds the query and does not issue the query to the server until the `result` method is called on it or until an iterator method like `next` is called.
@H_404_2@