簡體   English   中英

如何從Perl中的mongoDB返回數據?

[英]How to get returned data from mongoDB in Perl?

我閱讀了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";
}

此代碼不會產生任何輸出。 我想基本上遍歷整個集合並按文檔閱讀文檔。 不確定我做錯了什么。 我用過上面的代碼。 我更改了$ cur-> $ cursor->旁邊我認為這是一個錯字。 到目前為止,我很欣賞所有答案。

那不是官方文件。 前往CPAN:

迭代結果與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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM