perl 操作mongodb 集锦 demo

81 篇文章 2 订阅
use strict;
use MongoDB;
use MongoDB::OID;
use Data::Dumper;
use MongoDB::MongoClient;

my $client = MongoDB::MongoClient->new(
    host => 'mongodb://127.0.0.1:27017/admin',
    username => "",
    password => "",
);
$client->connect; 
my $database   = $client->get_database('Cnblogs');

my $collection = $database->get_collection('NoSql');

##############################处理1
#二个条件
#my $data       = $collection->find({'Name' =>'Nothing','Type' => 'Something'});输出1条
#my $data       = $collection->find({'age'=>25,'Name' =>'NothingX','Type'=>'SomethingXX'});输出1条
#查询<=,>=,<,>
#db.NoSql.find({'age': {'$lt': 24}});
#my $data       = $collection->find({'age' => {'$lte' => 24}});输出4条
#my $data       = $collection->find({'age' => {'$gte' => 24}});输出4条
#my $data       = $collection->find({'age' => {'$lt' => 25}});输出4条
#my $data       = $collection->find({'age' => {'$gt' => 24}});输出4条
#my $data       = $collection->find({'age' => {'$gt'=>20, '$lte'=> 25}});输出5条
##############################
##############################处理2
#db.NoSql.find({'age': {'$lt': 25}}, {'fields':{'age': 1,'sex':1}});
#查询指定列
#my $data       = $collection->find({'age' => {'$lte' => 25}})->fields( { 'age'=> 1,'sex'=>1 }); 输出 8条3列
#my $data       = $collection->find({'age' => {'$gt'=>20, '$lte'=> 25}})->fields( { 'age'=> 1 }); 输出 5条2列
#my $data       = $collection->find({'age' => {'$gt'=>20, '$lte'=> 25}})->fields( { 'age'=>1,'sex'=>1 }); 输出 5条 3列
#my $data       = $collection->find({'age' => {'$gt'=>20, '$lte'=> 25},'sex' => 'true'})->fields( { 'age'=>1,'sex'=>1 }); 输出 1条 3列
##############################
##############################处理3
#SELECT * FROM users WHERE a=1 or b=2
##db.NoSql.find({'$or': [{'Name':'NothingX'},{'age':25}]});
#my $data       = $collection->find({'$or'=> [{'sex' => 'true'},{'Name'=> 'NothingX'}]}); 输出6条
##############################
#db.NoSql.find({Name:'NothingX'}).limit(2);
#my $data       = $collection->find({'$or'=> [{'sex' => 'true'},{'Name'=> 'NothingX'},{'age'=> 25}]})->limit(2); 输出2条
#db.NoSql.find({Name:'Nothing',Type:'Something',vype:'ddddd'}).count()
#my $data       = $collection->find({'$or'=> [{'sex' => 'true'},{'Name'=> 'NothingX'},{'age'=> 21}]})->count;  输出:6
#my $data       = $collection->find({'$or'=> [{'sex' => 'true'},{'Name'=> 'NothingX'},{'age'=> 25}]})->count; 输出:8
#my $data       = $collection->find({'$or'=> [{'sex' => 'true'},{'Name'=> 'NothingX'},{'age'=> 25}]}); 输出8条
#my $data       = $collection->find({"age"=> {"\$lt"=> 21}})->count(); #输出:3
#my $data       = $collection->find()->count; 输出:8
#my $data       = $collection->find()->limit(4); #输出4条
#my $data       = $collection->find({ "Type" => "SomethingXX","Name"=> "NothingX"}); #输出1条
#my $data       = $collection->find({ "Type" => "SomethingXX","Name"=> "NothingX"}); #输出1条
#my $data       = $collection->find({"Name"=> "孔军"}); #输出1条
#my $data = $collection->count({'age'=>25,'Name' =>'NothingX','Type'=>'SomethingXX'});  输出: 11
#my $data = $collection->count({'age'=>25});  #输出:4
#my $data       = $collection->find({"age"=> {"\$gte"=>15,"\$lt"=> 21}}); #输出3条

my $data       = $collection->find({"age"=> {"\$gte"=>15,"\$lt"=> 21},"Name"=>"zhangsan"}); #输出2条

my @objects     = $data->all;

my $result = "";
    foreach my $myarry (@objects)
{
$result.="{";

foreach my $key ( keys %$myarry ){
$result.=" $key";
   $result.=":";
  $result.="${$myarry}{$key}".",";
}
$result = substr($result,0,length($result)-1);

$result.="}"."\n"; #去除最后一个,号
    }
    
print "$result";

print $data;


其中#备注说明我运行出的结果,mongodb的语法转化为perl语法是没问题的

如果有不懂的地方给我评论留言……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值