溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php中的mongodb select常用操作是什么

發(fā)布時間:2021-07-02 17:11:47 來源:億速云 閱讀:126 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“php中的mongodb select常用操作是什么”,在日常操作中,相信很多人在php中的mongodb select常用操作是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”php中的mongodb select常用操作是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

復(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); 
}

到此,關(guān)于“php中的mongodb select常用操作是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI