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 相关文章推荐
提升PHP执行速度全攻略(下)
Oct 09 PHP
小偷PHP+Html+缓存
Dec 20 PHP
PHP中for与foreach的区别分析
Mar 09 PHP
PHP数组无限分级数据的层级化处理代码
Dec 29 PHP
phpcms模块开发之swfupload的使用介绍
Apr 28 PHP
php Calender(日历)代码分享
Jan 03 PHP
PHP编程风格规范分享
Jan 15 PHP
Zend Framework教程之请求对象的封装Zend_Controller_Request实例详解
Mar 07 PHP
微信自定义菜单的创建/查询/取消php示例代码
Aug 05 PHP
PHP读取zip文件的方法示例
Nov 17 PHP
PHP中的函数声明与使用详解
May 27 PHP
swoole和websocket简单聊天室开发
Nov 18 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
Laravel接收前端ajax传来的数据的实例代码
2017/07/20 PHP
php二维数组按某个键值排序的实例讲解
2019/02/15 PHP
javascript 全选与全取消功能的实现代码
2012/12/23 Javascript
jQuery可见性过滤器:hidden和:visibility用法实例
2015/06/24 Javascript
jQuery实现鼠标经过弹出提示信息的地图热点效果
2015/08/07 Javascript
JavaScript给每一个li节点绑定点击事件的实现方法
2016/12/01 Javascript
详解js中常规日期格式处理、月历渲染和倒计时函数
2016/12/28 Javascript
windows 下安装nodejs 环境变量设置
2017/02/02 NodeJs
分分钟玩转Vue.js组件(二)
2017/03/01 Javascript
vue之nextTick全面解析
2017/05/17 Javascript
JS继承与闭包及JS实现继承的三种方式
2017/10/15 Javascript
ionic3实战教程之随机布局瀑布流的实现方法
2017/12/28 Javascript
JS实现左边列表移到到右边列表功能
2018/03/28 Javascript
浅析Vue项目中使用keep-Alive步骤
2018/07/27 Javascript
利用Node.js如何实现文件循环覆写
2019/04/05 Javascript
Vue实现滑动拼图验证码功能
2019/09/15 Javascript
JS获取表格视图所选行号的ids过程解析
2020/02/21 Javascript
[02:00]DAC2018主宣传片——龙征四海,剑问东方
2018/03/20 DOTA
[10:54]Team Spirit vs Navi
2018/06/07 DOTA
详解JavaScript编程中的window与window.screen对象
2015/10/26 Python
简述Python中的进程、线程、协程
2016/03/18 Python
用python3 返回鼠标位置的实现方法(带界面)
2019/07/05 Python
python hashlib加密实现代码
2019/10/17 Python
python+OpenCV实现车牌号码识别
2019/11/08 Python
50行Python代码实现视频中物体颜色识别和跟踪(必须以红色为例)
2019/11/20 Python
OpenCV里的imshow()和Matplotlib.pyplot的imshow()的实现
2019/11/25 Python
解决pytorch 模型复制的一些问题
2021/03/03 Python
css3实现多个元素依次显示效果
2017/12/12 HTML / CSS
美国高级工作服品牌:Carhartt
2018/01/25 全球购物
加拿大在线眼镜零售商:SmartBuyGlasses加拿大
2019/05/25 全球购物
医学专业大学生求职的自我评价
2013/11/27 职场文书
年度考核自我鉴定
2014/03/19 职场文书
锦旗标语大全
2014/06/23 职场文书
幼儿园母亲节活动总结
2015/02/10 职场文书
2015年毕业生实习评语
2015/03/25 职场文书
python数据分析之用sklearn预测糖尿病
2021/04/22 Python