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


Posted in PHP onSeptember 06, 2014

前面说到了mongodb安装,配置,集群,以及php的插入与更新等,请参考:mongodb。
下面说一下,mongodb select的常用操作

测试数据:

{ "_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、取表条数

> 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代码如下,按顺序对应的:

$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、取单条数据

> 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代码如下,按顺序对应的

$collection->findOne();  

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

3、find snapshot 游标

> 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代码如下:

/** 

* 注意: 

* 在我们做了find()操作,获得 $result 游标之后,这个游标还是动态的. 

* 换句话说,在我find()之后,到我的游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$result 获得. 

*/  

$result = $collection->find(array("auther"=>"李白"))->snapshot();  

foreach ($result as $id => $value) {  

 var_dump($value);  

}

4、自定义列显示

> 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代码如下,按顺序对应的:

$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、分页

> db.books.find().skip(1).limit(1);  //跳过第条,取一条  

{ "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 } 

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

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

6、排序

> 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代码如下:

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

7、模糊查询

> 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代码如下,按顺序对应的:

$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

> 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的一些常用操作,接下来,还会写一点。

PHP 相关文章推荐
攻克CakePHP系列一 连接MySQL数据库
Oct 22 PHP
PHP 观察者模式的实现代码
May 10 PHP
教你如何开启shopnc b2b2c 伪静态
Oct 21 PHP
THINKPHP2.0到3.0有哪些改进之处
Jan 04 PHP
thinkPHP学习笔记之安装配置篇
Mar 05 PHP
PHP遍历XML文档所有节点的方法
Mar 12 PHP
php截取指定2个字符之间字符串的方法
Apr 15 PHP
PHP安装memcached扩展笔记
May 28 PHP
又十个超级有用的PHP代码片段
Sep 24 PHP
php使用GD2绘制几何图形示例
Feb 15 PHP
PHP实现的redis主从数据库状态检测功能示例
Jul 20 PHP
PHP智能识别收货地址信息实例
Jan 05 PHP
CodeIgniter错误mysql_connect(): No such file or directory解决方法
Sep 06 #PHP
使用PHP和HTML5 FormData实现无刷新文件上传教程
Sep 06 #PHP
Windows下的PHP 5.3.x安装 Zend Guard Loader教程
Sep 06 #PHP
php获取一个变量的名字的方法
Sep 05 #PHP
Yii使用find findAll查找出指定字段的实现方法
Sep 05 #PHP
如何让thinkphp在模型中自动完成session赋值小教程
Sep 05 #PHP
php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码
Sep 04 #PHP
You might like
玩转图像函数库―常见图形操作
2006/09/03 PHP
php命令行用法入门实例教程
2014/10/27 PHP
解析arp病毒背后利用的Javascript技术附解密方法
2007/08/06 Javascript
JS 有趣的eval优化输入验证实例代码
2013/09/22 Javascript
JavaScript显示表单内元素数量的方法
2015/04/02 Javascript
详解原生JavaScript实现jQuery中AJAX处理的方法
2016/05/10 Javascript
Javascript操作表单实例讲解(下)
2016/06/20 Javascript
JavaScript比较当前时间是否在指定时间段内的方法
2016/08/02 Javascript
基于Angularjs+mybatis实现二级评论系统(仿简书)
2017/02/13 Javascript
JavaScript实现一个空中避难的小游戏
2017/06/06 Javascript
利用nvm管理多个版本的node.js与npm详解
2017/11/02 Javascript
angular动态表单制作
2018/02/23 Javascript
H5+C3+JS实现双人对战五子棋游戏(UI篇)
2020/05/28 Javascript
Javascript节流函数throttle和防抖函数debounce
2020/12/03 Javascript
python进阶教程之异常处理
2014/08/30 Python
Python中asyncore异步模块的用法及实现httpclient的实例
2016/06/28 Python
Python读取和处理文件后缀为.sqlite的数据文件(实例讲解)
2017/06/27 Python
python写一个md5解密器示例
2018/02/23 Python
Django框架使用富文本编辑器Uedit的方法分析
2018/07/31 Python
Python递归函数 二分查找算法实现解析
2019/08/12 Python
Python中生成一个指定长度的随机字符串实现示例
2019/11/06 Python
Python使用Chrome插件实现爬虫过程图解
2020/06/09 Python
Django:使用filter的pk进行多值查询操作
2020/07/15 Python
分享一枚pycharm激活码适用所有pycharm版本我的pycharm2020.2.3激活成功
2020/11/20 Python
python中子类与父类的关系基础知识点
2021/02/02 Python
HTML5 canvas基本绘图之绘制线条
2016/06/27 HTML / CSS
Hotels.com中国区:好订网
2016/08/18 全球购物
巴黎卡诗美国官方网站:始于1964年的头发头皮护理专家
2017/07/10 全球购物
《走一步再走一步》教学反思
2014/02/15 职场文书
机关单位人员学雷锋心得体会
2014/03/10 职场文书
《青山处处埋忠骨》教学反思
2014/04/22 职场文书
咖啡厅商业计划书
2014/09/15 职场文书
解除劳动合同证明书模板
2014/11/20 职场文书
小学新课改心得体会
2016/01/22 职场文书
Jupyter notebook 输出部分显示不全的解决方案
2021/04/24 Python
详解Go语言中配置文件使用与日志配置
2022/06/01 Golang