php中的mongodb select常用操作代码示例(3)


> db.books.find( { money: { $in: [ 20,30,90] } } );   //查找money等于20,30,90的数据 
{ "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 } 
{ "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 } 
 
> db.books.find( { auther: { $in: [ /^李/,/^钱/ ] } } );    //查找以李,钱开头的数据 
{ "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 } 
{ "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 } 
{ "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }

php代码如下,按顺序对应的:

复制代码 代码如下:


$param = array("money" => array('$in'=>array(20,30,90))); 
$result = $collection->find($param); 
foreach ($result as $id=>$value) { 
 var_dump($value); 

 
$param = array("auther" => array('$in'=>array(new MongoRegex('/^李/'),new MongoRegex('/^钱/')))); 
$result = $collection->find($param); 
foreach ($result as $id=>$value) { 
 var_dump($value); 
}

9、$or

复制代码 代码如下:


> db.books.find( { $or: [ { money: 20 }, { money: 80 } ] } );   //查找money等于20,80的数据 
{ "_id" : 1, "title" : "红楼梦", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 } 

php代码如下:

复制代码 代码如下:


$param = array('$or'=>array(array("money"=>20),array("money"=>80))); 
$result = $collection->find($param); 
foreach ($result as $id=>$value) { 
 var_dump($value); 
}

10、distinct

复制代码 代码如下:


> db.books.distinct( 'auther' ); 
[ "曹雪芹", "钱钟书", "李白" ] 
 
> db.books.distinct( 'auther' , { money: { $gt: 60 } }); 
[ "曹雪芹", "李白" ] 

php代码如下:

复制代码 代码如下:


$result = $curDB->command(array("distinct" => "books", "key" => "auther")); 
foreach ($result as $id=>$value) { 
 var_dump($value); 

 
$where = array("money" => array('$gte' => 60)); 
$result = $curDB->command(array("distinct" => "books", "key" => "auther", "query" => $where)); 
foreach ($result as $id=>$value) { 
 var_dump($value); 
}

先写到这儿,上面只是SELECT的一些常用操作,接下来,还会写一点。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c369b15f6c36943712ef5717b3140d77.html