php中的mongodb select常用操作代码示例
发布:smiling 来源: PHP粉丝网 添加日期:2021-04-10 23:20:40 浏览: 评论:0
这篇文章主要介绍了php中的mongodb select常用操作代码示例,本文同时包含mongodb操作命令和PHP实现代码,需要的朋友可以参考下
前面说到了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();
- > db.books.find().count();
- > db.books.count({auther: "李白" });
- > db.books.find({money:{$gt:40,$lte:60}}).count();
- > db.books.count({money:{$gt:40,$lte:60}});
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
- }
- //www.phpfensi.com
- > 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的一些常用操作,接下来,还会写一点。
Tags: mongodb
- 上一篇:php获取一个变量的名字的方法
- 下一篇:php实现的常见排序算法汇总
相关文章
- ·使用php mongodb扩展时比较需要注意的事项(2014-03-22)
- ·PHP实现GRIDFS存储文件到MONGODB的教程(2015-12-10)
- ·MONGODB GRIDFS存取文件PHP示例(2015-12-10)
- ·在MongoDB上如何进行数据的导入与导出(2020-03-22)
- ·MongoDB在PHP中的常用操作小结(2020-09-17)
- ·php实现Mongodb自定义方式生成自增ID的方法(2021-05-17)
- ·PHP实现的mongoDB数据库操作类完整实例(2021-09-08)
- ·PHP使用mongoclient简单操作mongodb数据库示例(2021-11-06)
- ·mongodb和php的用法详解(2021-11-14)
- ·PHP7 mongoDB扩展使用的方法分享(2021-11-20)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)