当前位置: 首页 > 编程笔记 >

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

曾光誉
2023-03-14
本文向大家介绍php中的mongodb select常用操作代码示例,包括了php中的mongodb select常用操作代码示例的使用技巧和注意事项,需要的朋友参考一下

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

 类似资料:
  • 不是说好的示例代码吗?怎么是个类名. 没错的,就是一个类哦,而且是一个完整的testcase. 它涵盖了95%以上的常用操作. 它的地址是 DaoUpTest 如果您访问github有"难度", 那么,还有osc上的镜像 DaoUpTest 如何使用 看它的注释,非常非常详细,几乎是一行代码一行注释. 例如准备说明 // 请在src或maven的resources下面添加一个文件叫nutz-te

  • 本文向大家介绍Java中使用Jedis操作Redis的示例代码,包括了Java中使用Jedis操作Redis的示例代码的使用技巧和注意事项,需要的朋友参考一下 使用Java操作Redis需要jedis-2.1.0.jar,下载地址:jedis-2.1.0.jar 如果需要使用Redis连接池的话,还需commons-pool-1.5.4.jar,下载地址:commons-pool-1.5.4.ja

  • 本文向大家介绍python操作链表的示例代码,包括了python操作链表的示例代码的使用技巧和注意事项,需要的朋友参考一下 以上就是python操作链表的示例代码的详细内容,更多关于Python链表的资料请关注呐喊教程其它相关文章!

  • 本文向大家介绍一波PHP中cURL库的常见用法代码示例,包括了一波PHP中cURL库的常见用法代码示例的使用技巧和注意事项,需要的朋友参考一下 php 的CURL是不错的功能,下面收藏几段不错的片段 0、基本例子 一般流程: 1、测试网站是否运行正常 2、可以代替file_gecontents的操作 3、保存某个网站下的所有图片 4、FTP应用 5、使用curl发送JSON数据

  • 本文向大家介绍在Java中操作Zookeeper的示例代码详解,包括了在Java中操作Zookeeper的示例代码详解的使用技巧和注意事项,需要的朋友参考一下 依赖 连接到zkServer 检测节点是否存在 操作后,服务端会返回处理结果,返回void、null也算处理结果。 同步指的是当前线程阻塞,等待服务端返回数据,收到返回的数据才继续往下执行; 异步回调指的是,把对结果(返回的数据)的处理写在

  • 本文向大家介绍spring使用redis操作key-value的示例代码,包括了spring使用redis操作key-value的示例代码的使用技巧和注意事项,需要的朋友参考一下 连接到 Redis Redis 连接工厂会生成到 Redis 数据库服务器的连接。Spring Data Redis 为四种 Redis 客户端实现提供了连接工厂: JedisConnectionFactory Jred