Chinaunix首页 | 论坛 | 博客
  • 博客访问: 193186
  • 博文数量: 29
  • 博客积分: 731
  • 博客等级: 上士
  • 技术积分: 435
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 16:18
文章分类

全部博文(29)

文章存档

2012年(29)

分类: Python/Ruby

2012-04-16 14:14:39

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;
    $db->users->find({age => 33}, {a => 1, b => 1});
33>>
    $db->users->find({age => {'$gt' => 33}})
<
    $db->users->find({name => qr/Joe/});
33 AND age<=40>>
    $db->users->find({age => {'$gt' => 33, '$lte' => 40}});
    $db->users->find({a => 1, b => "q"});
    $db->users->find({'$or' => [{a => 1}, {b => 2}]});
    $db->run_command({distinct => "users", key => "last_name"});
30>>
    $db->users->find({"age" => {'$gt' => 30}})->count;