mongodb select php操作 命令行操作

mongodb select php操作 命令行操作

张映 发表于 2014-09-05

分类目录: nosql, php

标签:$in, $nin, $or, distinct, fields, find, findone, like, limit, mongodb, php, select, skip

前面说到了mongodb安装,配置,集群,以及php的插入与更新等,请参考:mongodb

下面说一下,mongodb select的常用操作

测试数据

  1. "_id" : 1, "title" : "红楼梦""auther" : "曹雪芹""typeColumn" : "test""money" : 80, "code" : 10 }  
  2. "_id" : 2, "title" : "围城""auther" : "钱钟书""typeColumn" : "test""money" : 56, "code" : 20 }  
  3. "_id" : 3, "title" : "朝发白帝城""auther" : "李白""typeColumn" : "test""money" : 30, "code" : 30 }  
  4. "_id" : 4, "title" : "将近酒""auther" : "李白""money" : 90, "code" : 40 }  
{ "_id" : 1, "title" : "红楼梦", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }
{ "_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 }

1,取表条数

  1. > db.books.count();  
  2. 4  
  3.   
  4. > db.books.find().count();  
  5. 4  
  6.   
  7. > db.books.count({auther: "李白" });  
  8. 2  
  9.   
  10. > db.books.find({money:{$gt:40,$lte:60}}).count();  
  11. 1  
  12.   
  13. > db.books.count({money:{$gt:40,$lte:60}});  
  14. 1  
> db.books.count();
4

> db.books.find().count();
4

> db.books.count({auther: "李白" });
2

> db.books.find({money:{$gt:40,$lte:60}}).count();
1

> db.books.count({money:{$gt:40,$lte:60}});
1

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

  1. $collection->count();             //结果:4  
  2. $collection->find()->count();     //结果:4  
  3. $collection->count(array("auther"=>"李白"));   //结果:2  
  4. $collection->find(array("money"=>array('$gt'=>40,'$lte'=>60)))->count();     //结果:1  
  5. $collection->count(array("money"=>array('$gt'=>40,'$lte'=>60)));    //结果:1  
$collection->count();             //结果:4
$collection->find()->count();     //结果:4
$collection->count(array("auther"=>"李白"));   //结果:2
$collection->find(array("money"=>array('$gt'=>40,'$lte'=>60)))->count();     //结果:1
$collection->count(array("money"=>array('$gt'=>40,'$lte'=>60)));    //结果:1

提示:$gt为大于、$gte为大于等于、$lt为小于、$lte为小于等于、$ne为不等于、$exists不存在、$in指定范围、$nin指定不在某范围

2,取单条数据

  1. > db.books.findOne();  
  2. {  
  3.         "_id" : 1,  
  4.         "title" : "红楼梦",  
  5.         "auther" : "曹雪芹",  
  6.         "typeColumn" : "test",  
  7.         "money" : 80,  
  8.         "code" : 10  
  9. }  
  10.   
  11. > db.books.findOne({auther: "李白" });  
  12. {  
  13.         "_id" : 3,  
  14.         "title" : "朝发白帝城",  
  15.         "auther" : "李白",  
  16.         "typeColumn" : "test",  
  17.         "money" : 30,  
  18.         "code" : 30  
  19. }  
> db.books.findOne();
{
        "_id" : 1,
        "title" : "红楼梦",
        "auther" : "曹雪芹",
        "typeColumn" : "test",
        "money" : 80,
        "code" : 10
}

> db.books.findOne({auther: "李白" });
{
        "_id" : 3,
        "title" : "朝发白帝城",
        "auther" : "李白",
        "typeColumn" : "test",
        "money" : 30,
        "code" : 30
}

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

  1. $collection->findOne();  
  2. $collection->findOne(array("auther"=>"李白"));  
$collection->findOne();
$collection->findOne(array("auther"=>"李白"));

3,find snapshot 游标

  1. > db.books.find( { $query: {auther: "李白" }, $snapshot: true } );  
  2. "_id" : 3, "title" : "朝发白帝城""auther" : "李白""typeColumn" : "test""money" : 30, "code" : 30 }  
  3. "_id" : 4, "title" : "将近酒""auther" : "李白""money" : 90, "code" : 40 }  
> db.books.find( { $query: {auther: "李白" }, $snapshot: true } );
{ "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
{ "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }

php代码如下

  1. /** 
  2. * 注意: 
  3. * 在我们做了find()操作,获得 $result 游标之后,这个游标还是动态的. 
  4. * 换句话说,在我find()之后,到我的游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$result 获得. 
  5. */  
  6. $result = $collection->find(array("auther"=>"李白"))->snapshot();  
  7. foreach ($result as $id => $value) {  
  8.  var_dump($value);  
  9. }  
/**
* 注意:
* 在我们做了find()操作,获得 $result 游标之后,这个游标还是动态的.
* 换句话说,在我find()之后,到我的游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$result 获得.
*/
$result = $collection->find(array("auther"=>"李白"))->snapshot();
foreach ($result as $id => $value) {
 var_dump($value);
}

4,自定义列显示

  1. > db.books.find({},{"money":0,"auther":0});      //money和auther不显示  
  2. "_id" : 1, "title" : "红楼梦""typeColumn" : "test""code" : 10 }  
  3. "_id" : 2, "title" : "围城""typeColumn" : "test""code" : 20 }  
  4. "_id" : 3, "title" : "朝发白帝城""typeColumn" : "test""code" : 30 }  
  5. "_id" : 4, "title" : "将近酒""code" : 40 }  
  6.   
  7. > db.books.find({},{"title":1});          //只显示title列  
  8. "_id" : 1, "title" : "红楼梦" }  
  9. "_id" : 2, "title" : "围城" }  
  10. "_id" : 3, "title" : "朝发白帝城" }  
  11. "_id" : 4, "title" : "将近酒" }  
  12.   
  13. /** 
  14. *money在60到100之间,typecolumn和money二列必须存在 
  15. */  
  16. > db.books.find({money:{$gt:60,$lte:100}},{"typeColumn":1,"money":1});  
  17. "_id" : 1, "typeColumn" : "test""money" : 80 }  
  18. "_id" : 4, "money" : 90 }  
> db.books.find({},{"money":0,"auther":0});      //money和auther不显示
{ "_id" : 1, "title" : "红楼梦", "typeColumn" : "test", "code" : 10 }
{ "_id" : 2, "title" : "围城", "typeColumn" : "test", "code" : 20 }
{ "_id" : 3, "title" : "朝发白帝城", "typeColumn" : "test", "code" : 30 }
{ "_id" : 4, "title" : "将近酒", "code" : 40 }

> db.books.find({},{"title":1});          //只显示title列
{ "_id" : 1, "title" : "红楼梦" }
{ "_id" : 2, "title" : "围城" }
{ "_id" : 3, "title" : "朝发白帝城" }
{ "_id" : 4, "title" : "将近酒" }

/**
*money在60到100之间,typecolumn和money二列必须存在
*/
> db.books.find({money:{$gt:60,$lte:100}},{"typeColumn":1,"money":1});
{ "_id" : 1, "typeColumn" : "test", "money" : 80 }
{ "_id" : 4, "money" : 90 }

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

  1. $result = $collection->find()->fields(array("auther"=>false,"money"=>false));    //不显示auther和money列  
  2.   
  3. $result = $collection->find()->fields(array("title"=>true));      //只显示title列  
  4.   
  5. /** 
  6.  *money在60到100之间,typecolumn和money二列必须存在 
  7.  */  
  8. $where=array('typeColumn'=>array('$exists'=>true),'money'=>array('$exists'=>true,'$gte'=>60,'$lte'=>100));  
  9. $result = $collection->find($where);  
$result = $collection->find()->fields(array("auther"=>false,"money"=>false));    //不显示auther和money列

$result = $collection->find()->fields(array("title"=>true));      //只显示title列

/**
 *money在60到100之间,typecolumn和money二列必须存在
 */
$where=array('typeColumn'=>array('$exists'=>true),'money'=>array('$exists'=>true,'$gte'=>60,'$lte'=>100));
$result = $collection->find($where);

5,分页

  1. > db.books.find().skip(1).limit(1);  //跳过第条,取一条  
  2. "_id" : 2, "title" : "围城""auther" : "钱钟书""typeColumn" : "test""money" : 56, "code" : 20 }  
> db.books.find().skip(1).limit(1);  //跳过第条,取一条
{ "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }

这根mysql,limit,offset有点类似,php代码如下

  1. $result = $collection->find()->limit(1)->skip(1);//跳过 1 条记录,取出 1条  
$result = $collection->find()->limit(1)->skip(1);//跳过 1 条记录,取出 1条

6,排序

  1. > db.books.find().sort({money:1,code:-1});    //1表示降序 -1表示升序,参数的先后影响排序顺序   
  2. "_id" : 3, "title" : "朝发白帝城""auther" : "李白""typeColumn" : "test""money" : 30, "code" : 30 }  
  3. "_id" : 2, "title" : "围城""auther" : "钱钟书""typeColumn" : "test""money" : 56, "code" : 20 }  
  4. "_id" : 1, "title" : "红楼梦""auther" : "曹雪芹""typeColumn" : "test""money" : 80, "code" : 10 }  
  5. "_id" : 4, "title" : "将近酒""auther" : "李白""money" : 90, "code" : 40 }  
> db.books.find().sort({money:1,code:-1});    //1表示降序 -1表示升序,参数的先后影响排序顺序 
{ "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
{ "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
{ "_id" : 1, "title" : "红楼梦", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }
{ "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }

php代码如下

  1. $result = $collection->find()->sort(array('code'=>1,'money'=>-1));  
$result = $collection->find()->sort(array('code'=>1,'money'=>-1));

7,模糊查询

  1. > db.books.find({"title":/城/});      //like '%str%' 糊查询集合中的数据  
  2. "_id" : 2, "title" : "围城""auther" : "钱钟书""typeColumn" : "test""money" : 56, "code" : 20 }  
  3. "_id" : 3, "title" : "朝发白帝城""auther" : "李白""typeColumn" : "test""money" : 30, "code" : 30 }  
  4.   
  5. > db.books.find({"auther":/^李/});    //like 'str%' 糊查询集合中的数据  
  6. "_id" : 3, "title" : "朝发白帝城""auther" : "李白""typeColumn" : "test""money" : 30, "code" : 30 }  
  7. "_id" : 4, "title" : "将近酒""auther" : "李白""money" : 90, "code" : 40 }  
  8.   
  9. > db.books.find({"auther":/书$/});   //like '%str' 糊查询集合中的数据  
  10. "_id" : 2, "title" : "围城""auther" : "钱钟书""typeColumn" : "test""money" : 56, "code" : 20 }  
  11.   
  12. > db.books.find( { "title": { $regex'城'$options'i' } } );   //like '%str%' 糊查询集合中的数据  
  13. "_id" : 2, "title" : "围城""auther" : "钱钟书""typeColumn" : "test""money" : 56, "code" : 20 }  
  14. "_id" : 3, "title" : "朝发白帝城""auther" : "李白""typeColumn" : "test""money" : 30, "code" : 30 }  
> db.books.find({"title":/城/});      //like '%str%' 糊查询集合中的数据
{ "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
{ "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }

> db.books.find({"auther":/^李/});    //like 'str%' 糊查询集合中的数据
{ "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
{ "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }

> db.books.find({"auther":/书$/});   //like '%str' 糊查询集合中的数据
{ "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }

> db.books.find( { "title": { $regex: '城', $options: 'i' } } );   //like '%str%' 糊查询集合中的数据
{ "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
{ "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }

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

  1. $param = array("title" => new MongoRegex('/城/'));  
  2. $result = $collection->find($param);  
  3.   
  4. $param = array("auther" => new MongoRegex('/^李/'));  
  5. $result = $collection->find($param);  
  6.   
  7. $param = array("auther" => new MongoRegex('/书$/'));  
  8. $result = $collection->find($param);  
$param = array("title" => new MongoRegex('/城/'));
$result = $collection->find($param);

$param = array("auther" => new MongoRegex('/^李/'));
$result = $collection->find($param);

$param = array("auther" => new MongoRegex('/书$/'));
$result = $collection->find($param);

8,$in和$nin

  1. > db.books.find( { money: { $in: [ 20,30,90] } } );   //查找money等于20,30,90的数据  
  2. "_id" : 3, "title" : "朝发白帝城""auther" : "李白""typeColumn" : "test""money" : 30, "code" : 30 }  
  3. "_id" : 4, "title" : "将近酒""auther" : "李白""money" : 90, "code" : 40 }  
  4.   
  5. > db.books.find( { auther: { $in: [ /^李/,/^钱/ ] } } );    //查找以李,钱开头的数据  
  6. "_id" : 2, "title" : "围城""auther" : "钱钟书""typeColumn" : "test""money" : 56, "code" : 20 }  
  7. "_id" : 3, "title" : "朝发白帝城""auther" : "李白""typeColumn" : "test""money" : 30, "code" : 30 }  
  8. "_id" : 4, "title" : "将近酒""auther" : "李白""money" : 90, "code" : 40 }  
> 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代码如下,按顺序对应的

  1. $param = array("money" => array('$in'=>array(20,30,90)));  
  2. $result = $collection->find($param);  
  3. foreach ($result as $id=>$value) {  
  4.  var_dump($value);  
  5. }  
  6.   
  7. $param = array("auther" => array('$in'=>array(new MongoRegex('/^李/'),new MongoRegex('/^钱/'))));  
  8. $result = $collection->find($param);  
  9. foreach ($result as $id=>$value) {  
  10.  var_dump($value);  
  11. }  
$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

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

php代码如下

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

 10,distinct

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

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

php代码如下

  1. $result = $curDB->command(array("distinct" => "books""key" => "auther"));  
  2. foreach ($result as $id=>$value) {  
  3.  var_dump($value);  
  4. }  
  5.   
  6. $where = array("money" => array('$gte' => 60));  
  7. $result = $curDB->command(array("distinct" => "books""key" => "auther""query" => $where));  
  8. foreach ($result as $id=>$value) {  
  9.  var_dump($value);  
  10. }  
$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的一些常用操作,接下来,还会写一点。

 

 

0


转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/php/1644.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值