W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
(mongoDB >=1.0.0)
MongoDB\Driver\Manager::executeBulkWrite — 執(zhí)行一個或多個寫入操作
final public MongoDB\Driver\Manager::executeBulkWrite(string $namespace, MongoDB\Driver\BulkWrite $bulk, array|MongoDB\Driver\WriteConcern|null $options = null): MongoDB\Driver\WriteResult
在主服務(wù)器上執(zhí)行一個或多個寫入操作。
MongoDB\Driver\BulkWrite 可以使用 一個或多個不同類型的寫入操作(例如更新、刪除和 插入物)。驅(qū)動程序?qū)L試將相同類型的操作發(fā)送到 服務(wù)器在盡可能少的請求中優(yōu)化往返。
namespace
(字符串)完全限定的命名空間(例如 )。"databaseName.collectionName"
bulk
(MongoDB\驅(qū)動程序\批量寫入)要執(zhí)行的寫入。
options
選擇 | 類型 | 描述 |
---|---|---|
會期 | MongoDB\驅(qū)動程序\會話 | 要與操作關(guān)聯(lián)的會話。 |
寫關(guān)注點(diǎn) | MongoDB\驅(qū)動程序\WriteConcern | 要應(yīng)用于操作的寫入關(guān)注點(diǎn)。 |
成功后返回 MongoDB\Driver\WriteResult。
版本 | 說明 |
---|---|
PECL mongodb 1.4.4 | MongoDB\Driver\Exception\InvalidArgumentException,如果在 與未確認(rèn)的寫入問題相結(jié)合。"session" |
PECL mongodb 1.4.0 | 第三個參數(shù)現(xiàn)在是一個數(shù)組。 為了向后兼容,此參數(shù)仍將接受 MongoDB\Driver\WriteConcern 對象。options |
PECL mongodb 1.3.0 | MongoDB\Driver\Exception\InvalidArgumentException現(xiàn)在被拋出,如果不包含任何寫入 操作。以前,MongoDB\Driver\Exception\BulkWriteException 是 扔。bulk |
示例 #1 MongoDB\Driver\Manager::executeBulkWrite() example
<?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://localhost: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);
}
/* If the WriteConcern could not be fulfilled */
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n", $writeConcernError->getMessage(), $writeConcernError->getCode(), var_export($writeConcernError->getInfo(), true));
}
/* If a write could not happen at all */
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)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: