我读了
http://api.mongodb.org/perl/current/MongoDB/Examples.html,似乎只是来自Perl上mongoDB的文档.如何从perl中的mongoDB获取查询结果.让我们对Hash说.到目前为止,我已成功连接到数据库.我设法插入集合.现在我如何发出选择查询并将其返回的数据转换为哈希或类似的东西?
更新:
Example of my data { "_id" : ObjectId("asdhgajsdghajgh"),"country" : "USA" "city" : "Boston" } { "_id" : ObjectId("asdhgajsdghajgh"),"country" : "USA" "city" : "Seattle" } { "_id" : ObjectId("asdhgajsdghajgh"),"country" : "Canada" "city" : "Calgary" } My code my $cursor = $my_collection ->find({ country => 1 }) ; while (my $row = $cursor->next) { print "$row\n"; }
此代码不会产生任何输出.
我想基本上遍历整个集合并按文档阅读文档.
不确定我做错了什么.我用过上面的代码.我在$cursor->旁边更改了$cur->接下来我猜这是一个错字.到目前为止,我很欣赏所有答案.
解决方法
那不是官方文件.前往CPAN:
> MongoDB::Tutorial
> MongoDB::Examples
迭代结果与DBI方式非常相似:
use Data::Printer; use MongoDB; # no query is performed on initialization! my $cursor = $collection ->find({ active => 1,country => 'Canada' }) # filter "active" records from Canada ->sort({ stamp => -1 }) # order by "stamp" attribute,desc. ->limit(1000); # first 1000 records # query & iterate while (my $row = $cur->next) { # it is 'p',from Data::Printer! p $row; }