PHP Conference Japan 2024

MongoDB\Driver\Manager::executeBulkWrite

(mongodb >=1.0.0)

MongoDB\Driver\Manager::executeBulkWrite執行一個或多個寫入操作

描述

final public MongoDB\Driver\Manager::executeBulkWrite(string $namespace, MongoDB\Driver\BulkWrite $bulk, array|MongoDB\Driver\WriteConcern|null $options = null): MongoDB\Driver\WriteResult

在主要伺服器上執行一個或多個寫入操作。

可以使用一個或多個不同類型的寫入操作(例如,更新、刪除和插入)來建構 MongoDB\Driver\BulkWrite。驅動程式會盡可能以最少的請求將相同類型的操作傳送至伺服器,以最佳化往返次數。

"writeConcern" 選項的預設值會從活動中的交易(由 "session" 選項指示)推斷,然後從 連線 URI 推斷。

參數

namespace (string)

完整的命名空間(例如 "databaseName.collectionName")。

bulk (MongoDB\Driver\BulkWrite)

要執行的寫入操作。

options

options
選項 類型 描述
session MongoDB\Driver\Session

要與操作關聯的 Session。

writeConcern MongoDB\Driver\WriteConcern

要套用至操作的寫入關注。

傳回值

成功時傳回 MongoDB\Driver\WriteResult

錯誤/例外

變更日誌

版本 描述
PECL mongodb 1.4.4 如果 "session" 選項與未確認的寫入關注一起使用,則會拋出 MongoDB\Driver\Exception\InvalidArgumentException
PECL mongodb 1.4.0 第三個參數現在是一個 options 陣列。為了向後相容,此參數仍會接受 MongoDB\Driver\WriteConcern 物件。
PECL mongodb 1.3.0 如果 bulk 不包含任何寫入操作,現在會拋出 MongoDB\Driver\Exception\InvalidArgumentException。先前,會拋出 MongoDB\Driver\Exception\BulkWriteException

範例

範例 #1 MongoDB\Driver\Manager::executeBulkWrite() 範例

<?php

$bulk
= new MongoDB\Driver\BulkWrite();

$bulk->insert(['_id' => 1, 'x' => 1]);
$bulk->insert(['_id' => 2, 'x' => 2]);

$bulk->update(['x' => 2], ['$set' => ['x' => 1]], ['multi' => false, 'upsert' => false]);
$bulk->update(['x' => 3], ['$set' => ['x' => 3]], ['multi' => false, 'upsert' => true]);
$bulk->update(['_id' => 3], ['$set' => ['x' => 3]], ['multi' => false, 'upsert' => true]);

$bulk->insert(['_id' => 4, 'x' => 2]);

$bulk->delete(['x' => 1], ['limit' => 1]);

$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
$result = $manager->executeBulkWrite('db.collection', $bulk, $writeConcern);

printf("Inserted %d document(s)\n", $result->getInsertedCount());
printf("Matched %d document(s)\n", $result->getMatchedCount());
printf("Updated %d document(s)\n", $result->getModifiedCount());
printf("Upserted %d document(s)\n", $result->getUpsertedCount());
printf("Deleted %d document(s)\n", $result->getDeletedCount());

foreach (
$result->getUpsertedIds() as $index => $id) {
printf('upsertedId[%d]: ', $index);
var_dump($id);
}

/* 如果 WriteConcern 無法滿足 */
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n", $writeConcernError->getMessage(), $writeConcernError->getCode(), var_export($writeConcernError->getInfo(), true));
}

/* 如果寫入完全無法執行 */
foreach ($result->getWriteErrors() as $writeError) {
printf("Operation#%d: %s (%d)\n", $writeError->getIndex(), $writeError->getMessage(), $writeError->getCode());
}
?>

上面的範例會輸出類似以下的結果

Inserted 3 document(s)
Matched  1 document(s)
Updated  1 document(s)
Upserted 2 document(s)
Deleted  1 document(s)
upsertedId[3]: object(MongoDB\BSON\ObjectId)#5 (1) {
  ["oid"]=>
  string(24) "54d3adc3ce7a792f4d703756"
}
upsertedId[4]: int(3)
新增註解

使用者貢獻的註解 1 則註解

RJ
5 年前
如果您想知道如何只插入一個物件,而不是產生一堆不必要的 [0] 在您的 schema 中的陣列,我很抱歉我無法幫助您。我已經搜尋過 SERP,由於 MongoDB\Driver\BulkWrite 只接受陣列,似乎在 PHP7 中完全沒有辦法「insertOne」,也沒有辦法只傳遞一個物件而不傳遞陣列,這可能意味著您必須使用不同的語言或還原為 shell 命令 :-\。

有幾篇文章中,人們顯然錯誤地引用了「MongoDB\Driver\Client」,但無論是該程式碼還是「MongoDB\Driver\Collections」似乎都不存在。我感到非常沮喪,但如果這迫使我在一個全新的專案中降級到 PHP5,我只需要轉向更現代的語言。:-(

經過幾個小時的折磨後,我現在將再花一個小時嘗試弄清楚是否可以撰寫一個更新查詢,以折疊不必要地被塞在應該是物件(文件)位置的陣列。唉。我意識到使用 MongoDB 的人大多已經逃離 PHP,但是拋棄我們這些少數且忠實的使用者真的非常令人難過,正如多年來未解決的 stackoverflow 的哀嘆所證明的那樣。
To Top