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动态创建Web站点的方法
Aug 14 PHP
php页面缓存ob系列函数介绍
Oct 18 PHP
探讨fckeditor在Php中的配置详解
Jun 08 PHP
使用php实现截取指定长度
Aug 06 PHP
百度站点地图(百度sitemap)生成方法分享
Jan 09 PHP
神盾加密解密教程(三)PHP 神盾解密工具
Jun 08 PHP
php生成curl命令行的方法
Dec 14 PHP
PHP编写RESTful接口的方法
Feb 21 PHP
解读PHP中上传文件的处理问题
May 29 PHP
浅谈使用 Yii2 AssetBundle 中 $publishOptions 的正确姿势
Nov 08 PHP
PHP操作redis实现的分页列表,新增,删除功能封装类与用法示例
Aug 04 PHP
PHP获取真实IP及IP模拟方法解析
Nov 24 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
漫威DC即将合作联动,而双方早已经秘密开始
2020/04/09 欧美动漫
PHP中创建并处理图象
2006/10/09 PHP
如何用php生成扭曲及旋转的验证码图片
2013/06/07 PHP
PHP中单引号与双引号的区别分析
2014/08/19 PHP
DWZ刷新dialog解决方法
2013/03/03 Javascript
jquery实现可拖动DIV自定义保存到数据的实例
2013/11/20 Javascript
解决jquery实现的radio重新选中的问题
2015/07/03 Javascript
javascript实现数组内值索引随机化及创建随机数组的方法
2015/08/10 Javascript
JavaScript与jQuery实现的闪烁输入效果
2016/02/18 Javascript
JavaScript设计模式经典之命令模式
2016/02/24 Javascript
JS正则表达式判断有效数实例代码
2017/03/13 Javascript
Vue学习之路之登录注册实例代码
2017/07/06 Javascript
laravel5.3 vue 实现收藏夹功能实例详解
2018/01/21 Javascript
JS 验证码功能的三种实现方式
2018/11/26 Javascript
Vue 组件注册实例详解
2019/02/23 Javascript
使用watch在微信小程序中实现全局状态共享
2019/06/03 Javascript
解决Vue.js应用回退或刷新界面时提示用户保存修改问题
2019/11/24 Javascript
深入理解redux之compose的具体应用
2020/01/12 Javascript
[45:15]Optic vs VP 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
python如何将图片转换为字符图片
2020/08/19 Python
pandas string转dataframe的方法
2018/04/11 Python
解决Python安装时报缺少DLL问题【两种解决方法】
2019/07/15 Python
如何在Django配置文件里配置session链接
2019/08/06 Python
Numpy中对向量、矩阵的使用详解
2019/10/29 Python
使用Python的Turtle绘制哆啦A梦实例
2019/11/21 Python
pycharm运行scrapy过程图解
2019/11/22 Python
Tensorflow获取张量Tensor的具体维数实例
2020/01/19 Python
关于tf.reverse_sequence()简述
2020/01/20 Python
Python对Tornado请求与响应的数据处理
2020/02/12 Python
python tkinter GUI绘制,以及点击更新显示图片代码
2020/03/14 Python
Pytorch 使用 nii数据做输入数据的操作
2020/05/26 Python
Python 实现集合Set的示例
2020/12/21 Python
WoolOvers澳洲官方网站:英国针织服装公司
2018/05/13 全球购物
《玩具柜台前的孩子》教学反思
2014/02/13 职场文书
就业证明函
2015/06/17 职场文书
2015年三好一满意工作总结
2015/07/24 职场文书