백엔드 개발 PHP 튜토리얼 php中的mongodb select常用操作代码示例_PHP

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

May 31, 2016 pm 07:29 PM
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 

 
> 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":/城

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

CakePHP 날짜 및 시간

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

CakePHP 프로젝트 구성

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

CakePHP 파일 업로드

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

CakePHP 라우팅

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP 토론

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP 빠른 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법

See all articles