加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何从Perl中的mongoDB返回数据?

发布时间:2020-12-15 22:02:53 所属栏目:大数据 来源:网络整理
导读:我读了 http://api.mongodb.org/perl/current/MongoDB/Examples.html,似乎只是来自Perl上mongoDB的文档.如何从perl中的mongoDB获取查询结果.让我们对Hash说.到目前为止,我已成功连接到数据库.我设法插入集合.现在我如何发出选择查询并将其返回的数据转换为哈
我读了 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 "$rown";
}

此代码不会产生任何输出.
我想基本上遍历整个集合并按文档阅读文档.
不确定我做错了什么.我用过上面的代码.我在$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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读