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 $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";
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语法是没问题的
如果有不懂的地方给我评论留言……