下面說一下,mongodb select的常用操作測試數(shù)據(jù):
復(fù)制代碼 代碼如下:
{ "_id" : 1, "title" : "紅樓夢", "auther" : "曹雪芹", "typeColumn" : "te " /> 在线视频中文字幕,久久久无码精品无码国产人妻丝瓜,成人免费网址在线

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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

前面說到了mongodb安裝,配置,集群,以及php的插入與更新等,請參考:mongodb。
下面說一下,mongodb select的常用操作

測試數(shù)據(jù):
復(fù)制代碼 代碼如下:
{ "_id" : 1, "title" : "紅樓夢", "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 }

1、取表條數(shù)
復(fù)制代碼 代碼如下:
> 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代碼如下,按順序?qū)?yīng)的:
復(fù)制代碼 代碼如下:
$collection->count();             //結(jié)果:4 
$collection->find()->count();     //結(jié)果:4 
$collection->count(array("auther"=>"李白"));   //結(jié)果:2 
$collection->find(array("money"=>array('$gt'=>40,'$lte'=>60)))->count();     //結(jié)果:1 
$collection->count(array("money"=>array('$gt'=>40,'$lte'=>60)));    //結(jié)果:1 

提示:$gt為大于、$gte為大于等于、$lt為小于、$lte為小于等于、$ne為不等于、$exists不存在、$in指定范圍、$nin指定不在某范圍

2、取單條數(shù)據(jù)

復(fù)制代碼 代碼如下:
> db.books.findOne(); 

        "_id" : 1, 
        "title" : "紅樓夢", 
        "auther" : "曹雪芹", 
        "typeColumn" : "test", 
        "money" : 80, 
        "code" : 10 

 
> db.books.findOne({auther: "李白" }); 

        "_id" : 3, 
        "title" : "朝發(fā)白帝城", 
        "auther" : "李白", 
        "typeColumn" : "test", 
        "money" : 30, 
        "code" : 30 

php代碼如下,按順序?qū)?yīng)的
復(fù)制代碼 代碼如下:
$collection->findOne(); 
$collection->findOne(array("auther"=>"李白")); 

3、find snapshot 游標(biāo)

復(fù)制代碼 代碼如下:
> 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代碼如下:
復(fù)制代碼 代碼如下:
/**
* 注意:
* 在我們做了find()操作,獲得 $result 游標(biāo)之后,這個游標(biāo)還是動態(tài)的.
* 換句話說,在我find()之后,到我的游標(biāo)循環(huán)完成這段時間,如果再有符合條件的記錄被插入到collection,那么這些記錄也會被$result 獲得.
*/ 
$result = $collection->find(array("auther"=>"李白"))->snapshot(); 
foreach ($result as $id => $value) { 
 var_dump($value); 
}

4、自定義列顯示

復(fù)制代碼 代碼如下:
> 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" : "朝發(fā)白帝城", "typeColumn" : "test", "code" : 30 } 
{ "_id" : 4, "title" : "將近酒", "code" : 40 } 
 
> db.books.find({},{"title":1});          //只顯示title列 
{ "_id" : 1, "title" : "紅樓夢" } 
{ "_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代碼如下,按順序?qū)?yīng)的:

復(fù)制代碼 代碼如下:
$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、分頁

復(fù)制代碼 代碼如下:
> db.books.find().skip(1).limit(1);  //跳過第條,取一條 
{ "_id" : 2, "title" : "圍城", "auther" : "錢鐘書", "typeColumn" : "test", "money" : 56, "code" : 20 } 

這根mysql,limit,offset有點類似,php代碼如下:
復(fù)制代碼 代碼如下:
$result = $collection->find()->limit(1)->skip(1);//跳過 1 條記錄,取出 1條 

6、排序

復(fù)制代碼 代碼如下:
> db.books.find().sort({money:1,code:-1});    //1表示降序 -1表示升序,參數(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" : "紅樓夢", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 } 
{ "_id" : 4, "title" : "將近酒", "auther" : "李白", "money" : 90, "code" : 40 }

php代碼如下:
復(fù)制代碼 代碼如下:
$result = $collection->find()->sort(array('code'=>1,'money'=>-1)); 

7、模糊查詢

復(fù)制代碼 代碼如下:
> 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代碼如下,按順序?qū)?yīng)的:

復(fù)制代碼 代碼如下:
$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

復(fù)制代碼 代碼如下:
> 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代碼如下,按順序?qū)?yīng)的:

復(fù)制代碼 代碼如下:
$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

復(fù)制代碼 代碼如下:
> db.books.find( { $or: [ { money: 20 }, { money: 80 } ] } );   //查找money等于20,80的數(shù)據(jù) 
{ "_id" : 1, "title" : "紅樓夢", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 } 

php代碼如下:
復(fù)制代碼 代碼如下:
$param = array('$or'=>array(array("money"=>20),array("money"=>80))); 
$result = $collection->find($param); 
foreach ($result as $id=>$value) { 
 var_dump($value); 
}

10、distinct

復(fù)制代碼 代碼如下:
> db.books.distinct( 'auther' ); 
[ "曹雪芹", "錢鐘書", "李白" ] 
 
> db.books.distinct( 'auther' , { money: { $gt: 60 } }); 
[ "曹雪芹", "李白" ] 

php代碼如下:

復(fù)制代碼 代碼如下:
$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技術(shù)php中的mongodb select常用操作代碼示例,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 神马电影我不卡4k手机在线观看 | 在线免费视频国产 | 亚洲中字幕永久在线观看 | 女同给老师下媚药 | 樱花草在线影视WWW日本动漫 | 欧美一区二区VA毛片视频 | 24小时日本在线电影 | 午夜伦理伦理片在线观 | 亚洲区欧美日韩综合 | 久久成人无码国产免费播放 | 国产精品久久免费视频 | A片毛片免费视频在线看 | 94色94色永久网站 | 99久久中文字幕伊人情人 | 国产精品一区二区制服丝袜 | 在线AV国产传媒18精品免费 | 美美哒高清在线播放8 | 国产成人久久精品AV | 欧美最猛黑人XXXXWWW | 免费在线看视频 | 大稥焦伊人一本dao 大香伊人中文字幕精品 | 国产互换后人妻的疯狂VIDEO | 亚洲AV综合99一二三四区 | 手机在线国产视频 | 国产全部视频列表支持手机 | 人妻体内射精一区二区 | 精品极品三大极久久久久 | 99久久国产露脸精品麻豆 | 中国女人hd| 国产a级黄色毛片 | 国产麻豆福利AV在线观看 | 最近最新中文字幕MV高清在线 | 阿片在线播放 | 最近中文字幕在线中文高清版 | 国产精品资源网站在线观看 | 亚洲精品偷拍影视在线观看 | 日韩一区精品视频一区二区 | 精品熟女少妇AV久久免费A片 | 国产精品色无码AV在线观看 | 国内精品免费视频精选在线观看 | 99爱在线观看 |