《PHP應(yīng)用:php中的mongodb select常用操作代碼示例》要點(diǎn):
本文介紹了PHP應(yīng)用:php中的mongodb select常用操作代碼示例,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP學(xué)習(xí)前面說到了mongodb安裝,設(shè)置裝備擺設(shè),集群,以及php的插入與更新等,請(qǐng)參考:mongodb.
下面說一下,mongodb select的常用操作
PHP進(jìn)修測(cè)試數(shù)據(jù):
代碼如下:
{ "_id" : 1, "title" : "紅樓夢(mèng)", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }?
{ "_id" : 2, "title" : "圍城", "auther" : "錢鐘書", "typeColumn" : "test", "money" : 56, "code" : 20 }?
{ "_id" : 3, "title" : "朝發(fā)白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }?
{ "_id" : 4, "title" : "快要酒", "auther" : "李白", "money" : 90, "code" : 40 }
PHP學(xué)習(xí)1、取表?xiàng)l數(shù)
代碼如下:
> 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學(xué)習(xí)php代碼如下,按次序?qū)?yīng)的:
代碼如下:
$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?
PHP學(xué)習(xí)提示:$gt為大于、$gte為大于等于、$lt為小于、$lte為小于等于、$ne為不等于、$exists不存在、$in指定范圍、$nin指定不在某范圍
PHP進(jìn)修2、取單條數(shù)據(jù)
代碼如下:
> db.books.findOne();?
{?
??????? "_id" : 1,?
??????? "title" : "紅樓夢(mèng)",?
??????? "auther" : "曹雪芹",?
??????? "typeColumn" : "test",?
??????? "money" : 80,?
??????? "code" : 10?
}?
?
> db.books.findOne({auther: "李白" });?
{?
??????? "_id" : 3,?
??????? "title" : "朝發(fā)白帝城",?
??????? "auther" : "李白",?
??????? "typeColumn" : "test",?
??????? "money" : 30,?
??????? "code" : 30?
}?
PHP學(xué)習(xí)php代碼如下,按次序?qū)?yīng)的
代碼如下:
$collection->findOne();?
$collection->findOne(array("auther"=>"李白"));?
PHP進(jìn)修3、find snapshot 游標(biāo)
代碼如下:
> db.books.find( { $query: {auther: "李白" }, $snapshot: true } );?
{ "_id" : 3, "title" : "朝發(fā)白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }?
{ "_id" : 4, "title" : "快要酒", "auther" : "李白", "money" : 90, "code" : 40 }
PHP學(xué)習(xí)php代碼如下:
代碼如下:
/**
* 注意:
* 在我們做了find()操作,得到 $result 游標(biāo)之后,這個(gè)游標(biāo)還是動(dòng)態(tài)的.
* 換句話說,在我find()之后,到我的游標(biāo)循環(huán)完成這段時(shí)間,如果再有符合條件的記錄被插入到collection,那么這些記錄也會(huì)被$result 得到.
*/?
$result = $collection->find(array("auther"=>"李白"))->snapshot();?
foreach ($result as $id => $value) {?
?var_dump($value);?
}
PHP學(xué)習(xí)4、自定義列顯示
代碼如下:
> db.books.find({},{"money":0,"auther":0});????? //money和auther不顯示?
{ "_id" : 1, "title" : "紅樓夢(mèng)", "typeColumn" : "test", "code" : 10 }?
{ "_id" : 2, "title" : "圍城", "typeColumn" : "test", "code" : 20 }?
{ "_id" : 3, "title" : "朝發(fā)白帝城", "typeColumn" : "test", "code" : 30 }?
{ "_id" : 4, "title" : "將近酒", "code" : 40 }?
?
> db.books.find({},{"title":1});????????? //只顯示title列?
{ "_id" : 1, "title" : "紅樓夢(mèng)" }?
{ "_id" : 2, "title" : "圍城" }?
{ "_id" : 3, "title" : "朝發(fā)白帝城" }?
{ "_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學(xué)習(xí)php代碼如下,按順序?qū)?yīng)的:
代碼如下:
$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);?
PHP學(xué)習(xí)5、分頁
代碼如下:
> db.books.find().skip(1).limit(1);? //跳過第條,取一條?
{ "_id" : 2, "title" : "圍城", "auther" : "錢鐘書", "typeColumn" : "test", "money" : 56, "code" : 20 }?
PHP學(xué)習(xí)這根mysql,limit,offset有點(diǎn)相似,php代碼如下:
代碼如下:
$result = $collection->find()->limit(1)->skip(1);//跳過 1 條記載,取出 1條?
PHP學(xué)習(xí)6、排序
代碼如下:
> db.books.find().sort({money:1,code:-1});??? //1表現(xiàn)降序 -1表現(xiàn)升序,參數(shù)的先后影響排序順序??
{ "_id" : 3, "title" : "朝發(fā)白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }?
{ "_id" : 2, "title" : "圍城", "auther" : "錢鐘書", "typeColumn" : "test", "money" : 56, "code" : 20 }?
{ "_id" : 1, "title" : "紅樓夢(mèng)", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }?
{ "_id" : 4, "title" : "將近酒", "auther" : "李白", "money" : 90, "code" : 40 }
PHP學(xué)習(xí)php代碼如下:
代碼如下:
$result = $collection->find()->sort(array('code'=>1,'money'=>-1));?
PHP學(xué)習(xí)7、隱約查詢
代碼如下:
> db.books.find({"title":/城/});????? //like '%str%' 糊查詢集合中的數(shù)據(jù)?
{ "_id" : 2, "title" : "圍城", "auther" : "錢鐘書", "typeColumn" : "test", "money" : 56, "code" : 20 }?
{ "_id" : 3, "title" : "朝發(fā)白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }?
?
> db.books.find({"auther":/^李/});??? //like 'str%' 糊查詢集合中的數(shù)據(jù)?
{ "_id" : 3, "title" : "朝發(fā)白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }?
{ "_id" : 4, "title" : "將近酒", "auther" : "李白", "money" : 90, "code" : 40 }?
?
> db.books.find({"auther":/書$/});?? //like '%str' 糊查詢集合中的數(shù)據(jù)?
{ "_id" : 2, "title" : "圍城", "auther" : "錢鐘書", "typeColumn" : "test", "money" : 56, "code" : 20 }?
?
> db.books.find( { "title": { $regex: '城', $options: 'i' } } );?? //like '%str%' 糊查詢集合中的數(shù)據(jù)?
{ "_id" : 2, "title" : "圍城", "auther" : "錢鐘書", "typeColumn" : "test", "money" : 56, "code" : 20 }?
{ "_id" : 3, "title" : "朝發(fā)白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }?
PHP學(xué)習(xí)php代碼如下,按次序?qū)?yīng)的:
代碼如下:
$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);?
PHP進(jìn)修8、$in和$nin
代碼如下:
> db.books.find( { money: { $in: [ 20,30,90] } } );?? //查找money等于20,30,90的數(shù)據(jù)?
{ "_id" : 3, "title" : "朝發(fā)白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }?
{ "_id" : 4, "title" : "將近酒", "auther" : "李白", "money" : 90, "code" : 40 }?
?
> db.books.find( { auther: { $in: [ /^李/,/^錢/ ] } } );??? //查找以李,錢開頭的數(shù)據(jù)?
{ "_id" : 2, "title" : "圍城", "auther" : "錢鐘書", "typeColumn" : "test", "money" : 56, "code" : 20 }?
{ "_id" : 3, "title" : "朝發(fā)白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }?
{ "_id" : 4, "title" : "將近酒", "auther" : "李白", "money" : 90, "code" : 40 }
PHP學(xué)習(xí)php代碼如下,按次序?qū)?yīng)的:
代碼如下:
$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);?
}
PHP進(jìn)修9、$or
代碼如下:
> db.books.find( { $or: [ { money: 20 }, { money: 80 } ] } );?? //查找money即是20,80的數(shù)據(jù)?
{ "_id" : 1, "title" : "紅樓夢(mèng)", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }?
PHP學(xué)習(xí)php代碼如下:
代碼如下:
$param = array('$or'=>array(array("money"=>20),array("money"=>80)));?
$result = $collection->find($param);?
foreach ($result as $id=>$value) {?
?var_dump($value);?
}
PHP進(jìn)修10、distinct
代碼如下:
> db.books.distinct( 'auther' );?
[ "曹雪芹", "錢鐘書", "李白" ]?
?
> db.books.distinct( 'auther' , { money: { $gt: 60 } });?
[ "曹雪芹", "李白" ]?
PHP進(jìn)修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);?
}
PHP進(jìn)修先寫到這兒,上面只是SELECT的一些常用操作,接下來,還會(huì)寫一點(diǎn).
歡迎參與《PHP應(yīng)用:php中的mongodb select常用操作代碼示例》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/14741.html