標籤:mon 字串 arc tor signature server 文檔 儲存 正序
一、軟體環境(版本非必須) php v5.6 擴充:MongoDB nginx v1.11 mongodb v3.2
note: 必須安裝MongoDB擴充
二、串連 $client = new MongoClient($server, $option);
$server 變數是個字串,描述要已連線的服務器
mongodb://[username:[email protected]]host1[:port1][,host2[:port2:],...]/db
其中必要的是:
username 資料庫使用者名稱 password 資料庫密碼 host 伺服器位址 port 資料庫監聽連接埠 三、選表(集合) 選擇資料庫 選擇集合 $db = $client->{$db_name};
返回MongoDB類 類文檔傳送門
$collection = $db->{$collection_name};
返回MongoCollection類 類文檔傳送門
四、查詢資料 $cursor = $collection->find($filter);
返回MongoCursor類 類文檔傳送門
1. $filter參數
mongoshell find函數參數使用:傳送門
php代碼編寫
// 查詢值大於value的文檔 $filter[$gtKey] = array(‘$gt‘ => $value); // regexKey模糊查詢 $filter[$regexKey] = new MongoRegex(‘/‘.$regexValue.‘/‘); $collection->find($filter);
注意:
模糊查詢需要使用MongoRegex類來使用,否則不識別 比較大小時,$value 的值需要與mongodb中儲存的類型一致 2. 利用MongoCursor進行分頁等操作 // 返回MongoCursor,可以利用MongoCursor的方法遍曆資料 // 或者使用iterator_to_array($cursor)轉換成數組 $cursor = $cursor ->sort(array($field=>‘-1 or 1‘)) // 1是正序,-1是反序 ->skip($start) // 資料起始位置 ->limit($size); // 資料結束位置為$start+$size // 返回int型數值 $count = $cursor ->find($filter) ->count(); $data = iterator_to_array($cursor)
不再講解如何使用MongoCursor遍曆資料
五、總結
使用php串連MongoDB查詢資料總體來說非常簡單。串連、根據條件查詢、分頁,三步搞定。
注意事項:
使用單例模式擷取MongoDB執行個體 模糊查詢需要藉助MongoRegex類 操作完成要** 關閉串連 **
That’s right. This article was first published in March 2016, buttimes change, new themes are launched for sale, so we’ve stripped out themes which are no longer available andadded eight new examples to keep things up to date.
1. Introduce Yourself
Emotional design is hugely important on the web; add the “human element” to your portfolio and your visitors will more likely connect with the work you show them. Make sure they know who they’re dealing with, who’s responsible for the photos, and who it is they’ll need to get in touch with if they want to know more.
The Responsive Photography Theme by ThemeGoods offers a homepage layout which does this really well. Your name, your signature, a touch of personality up front and centre.
PHP 從 MongoDb 中查詢資料怎麼樣實現