如何在mongodb中连接db并从perl中的集合中获取数据

时间:2016-01-19 14:13:13

标签: mongodb perl

我使用过perl 5.16 V和MongoDB 3.2.1版本。 我使用下面的代码但数据没有获取。我的代码中有任何问题吗?

############################################
stored data in collection tbl_ads
> db.tbl_ads.find()
{ "_id" : "103607835" "title" : "Massief Grenen Parket Winterprijs -50","user_id" : "11", "website_id" : "1" }
##########################################################
use strict;
use warnings;
use 5.010;

use MongoDB;
use Data::Dumper;


my $client = MongoDB::Connection->new;
my $db   = $client->get_database( 'temp_db' );

my $people_coll = $db->get_collection("tbl_ads");
my $ad_hash = $people_coll->find({"user_id"=>"11"});
print Dumper $ad_hash;

1 个答案:

答案 0 :(得分:1)

你遗失了很多东西,让我试着帮助你:

$client = MongoDB::MongoClient->new("host" => "your-server-here:port");
$client->connect;

# Not sure if you are using auth or not, you need to specify the collection you authenticate against
$client->authenticate('user-collection', 'user', 'pass');

$database = $client -> get_database('temp_db');
$document = $database -> get_collection('tbl_ads')->find_one({"user_id"=>"11"});

print Dumper $document;