搜尋
首頁php教程php手册php中的mongodb select常用操作代码示例,mongodbselect

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

前面说到了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 

 
> 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操作mongoDB数据库查询的时怎写“或”这样的多个条件查询代码?

据我所知,目前mongoDB没有“或”这个东西

但我刚才在网上查了下
发现了下面的信息,你参考下吧

在mongodb中有$or 操作符的,官网中给出的例子如下:

Simple:

db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )

With another field

db.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )

The $or operator retrieves matches for each or clause individually and eliminates duplicates when returning results. A number of $or optimizations are planned for 1.8. See this thread for details.
$or cannot be nested.
 

PHP中怎实现一个select语句读出多个表的内容

复合查询?例如。详细的意思不用我解说了吧
$sql="select * from p_newsbase as a, p_newscontent as b where a.id=b.nid and a.id='$_GET[id]'"
 

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境