php中的mongodb select常用作業碼樣本【轉載】

來源:互聯網
上載者:User

標籤:and   插入   條件   exist   開頭   off   god   配置   mysq   

前面說到了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的一些常用操作,接下來,還會寫一點。

php中的mongodb select常用作業碼樣本【轉載】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.