cpan安装MongoDB:
perl -MCPAN -e 'install MongoDB'
example:
use MongoDB;
my $connection = MongoDB::Connection->new( host => 'localhost', port => 27017 );
my $database = $connection->dbname;
my $collection = $database->tablename;
my $data = $collection->find({'key' => 'value'});
//select * from table where key = 'value' or $collection->find_one() limit 1
while (my $object = $data->next()) {
foreach my $key (keys %{$object}) {
say "$key => ${$object}{$key}";
}
}
$collection->update({'key' => "value"}, {'$set' => { 'mykey' => 'myvalue'}});
//update table set mykey = 'myvalue' where key = 'value'
一些sql语句的例子:
Implicit, can be done explicitly.
$db->users->insert({a => 1, b => 1});
$db->users->find({}, {a => 1, b => 1});
$db->users->find;
$db->users->find({age => 33})
$db->users->find({age => 33}, {a => 1, b => 1});
$db->users->find({age => 33})->sort({name => 1});
<
$db->users->find({age => {'$gt' => 33}})
<
$db->users->find({age => {'$lt' => 33}})
$db->users->find({name => qr/Joe/});
$db->users->find({name => qr/^Joe/});
<
$db->users->find({age => {'$gt' => 33, '$lte' => 40}});
$db->users->find->sort({name => -1});
$db->users->ensure_index({name => 1});
$db->users->ensure_index(Tie::IxHash->new(name => 1, ts => -1));
$db->users->find({a => 1, b => "q"});
$db->users->find->limit(10)->skip(20);
$db->users->find({'$or' => [{a => 1}, {b => 2}]});
$db->users->find->limit(1);
$db->users->find({z => 3})->explain;
$db->run_command({distinct => "users", key => "last_name"});
$db->users->count;
<
$db->users->find({"age" => {'$gt' => 30}})->count;
$db->users->find({age => {'$exists' => 1}})->count;
$db->users->update({b => "q"}, {'$set' => {a => 1}});
$db->users->update({b => "q"}, {'$inc' => {a => 2}});
$db->users->remove({z => "abc"});
阅读(4057) | 评论(1) | 转发(0) |