php簡單操作mongodb

2021-09-27 12:36:39 字數 1724 閱讀 8085

一:插入資料

例:向預設的test資料庫的wj表中插入資料

$manager = new mongodb\driver\manager("mongodb://localhost:27017");  

// 插入資料

$bulk = new mongodb\driver\bulkwrite;

$manager->executebulkwrite('test.wj', $bulk);

2:查詢資料

查詢test資料庫的wj表中name值不為測試的資料

$manager = new mongodb\driver\manager("mongodb://localhost:27017");  

$filter = ['name' => '測試'];

$options = [

'projection' => ['_id' => 0],

'sort' => ['name' => -1],

];$query = new mongodb\driver\query($filter, $options);

$cursor = $manager->executequery('test.wj', $query);

foreach ($cursor as $document)

3:更新資料

將test資料庫中的wj表中id為1的資料中name欄位變為測試1234,

$bulk = new mongodb\driver\bulkwrite;

$bulk->update(

['id' => 1],

['$set' => ['name' => '測試1234']],

['multi' => false, 'upsert' => false]  #multi表示只更新一條資料,upsert表示如果不存在update的記錄,不進行插入操作

);$manager = new mongodb\driver\manager("mongodb://localhost:27017");  

$writeconcern = new mongodb\driver\writeconcern(mongodb\driver\writeconcern::majority, 1000);

$result = $manager->executebulkwrite('test.wj', $bulk, $writeconcern);

4:刪除資料

$bulk = new mongodb\driver\bulkwrite;

$bulk->delete(['id' => 1], ['limit' => 1]);   // limit 為 1 時,刪除第一條匹配資料

$bulk->delete(['id' => 2], ['limit' => 0]);   // limit 為 0 時,刪除所有匹配資料

$manager = new mongodb\driver\manager("mongodb://localhost:27017");  

$writeconcern = new mongodb\driver\writeconcern(mongodb\driver\writeconcern::majority, 1000);

$result = $manager->executebulkwrite('test.wj', $bulk, $writeconcern);

mongoDB 簡單操作

一 新增資料庫 usedatabase name 例如 use tom database 當tom database不存在時會新建資料庫tom databse,當tom database存在時切換到tom database資料庫。當前資料庫的名稱可以通過db命令檢視。說明 執行上一語句時,資料庫並沒...

MongoDB 簡單操作

在 mongodb 資料庫中存在資料庫的概念,但是沒有模式 所有的資訊都是按照文件儲存的 資料結構為 json 結構,只不過在進行一些資料處理的時候才會使用到 mongodb 的一些操作符。1 使用 mldn 資料庫 use mldn 這個時候並不會建立資料庫,只有在資料庫裡面儲存集合資料之後才會建...

php簡單查詢mongoDB

以前都是玩mysql的,從這週開始接觸mongodb,並且用php查了一波mongodb。其實和mysql差不多,只是換了乙個資料庫,所以查詢語句的寫法不一樣。在這裡簡單總結一下。一 mongodb mysql是關聯式資料庫,mongodb是非關聯式資料庫,nosql not only sql 我以...