mongodb select php操作 命令行操作
前面说到了mongodb安装,配置,集群,以及php的插入与更新等,请参考: mongodb 。 下面说一下,mongodb select的常用操作 测试数据 { "_id" : 1, "title" : "红楼梦", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }{ "_id" : 2
前面说到了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的一些常用操作,接下来,还会写一点。



熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。

本文介紹如何在Debian系統上構建高可用性的MongoDB數據庫。我們將探討多種方法,確保數據安全和服務持續運行。關鍵策略:副本集(ReplicaSet):利用副本集實現數據冗餘和自動故障轉移。當主節點出現故障時,副本集會自動選舉新的主節點,保證服務的持續可用性。數據備份與恢復:定期使用mongodump命令進行數據庫備份,並製定有效的恢復策略,以應對數據丟失風險。監控與報警:部署監控工具(如Prometheus、Grafana)實時監控MongoDB的運行狀態,並

在PHP8 中,match表達式是一種新的控制結構,用於根據表達式的值返回不同的結果。 1)它類似於switch語句,但返回值而非執行語句塊。 2)match表達式使用嚴格比較(===),提升了安全性。 3)它避免了switch語句中可能的break遺漏問題,增強了代碼的簡潔性和可讀性。

本文介紹如何在Debian系統上配置MongoDB實現自動擴容,主要步驟包括MongoDB副本集的設置和磁盤空間監控。一、MongoDB安裝首先,確保已在Debian系統上安裝MongoDB。使用以下命令安裝:sudoaptupdatesudoaptinstall-ymongodb-org二、配置MongoDB副本集MongoDB副本集確保高可用性和數據冗餘,是實現自動擴容的基礎。啟動MongoDB服務:sudosystemctlstartmongodsudosys

在PHP中可以通過使用不可預測的令牌來有效防範CSRF攻擊。具體方法包括:1.生成並在表單中嵌入CSRF令牌;2.在處理請求時驗證令牌的有效性。

PHP中的...(splat)操作符用於函數參數和數組解包,提升代碼簡潔性和效率。 1)函數參數解包:將數組元素作為參數傳遞給函數。 2)數組解包:將一個數組解包到另一個數組中或作為函數參數。
