PHP8 MongoDB\Driver\Manager::executeBulkWrite

2024-04-08 10:15 更新

(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)化往返。

參數(shù) 

namespace (字符串)

完全限定的命名空間(例如 )。"databaseName.collectionName"

bulk (MongoDB\驅(qū)動程序\批量寫入)

要執(zhí)行的寫入。

options
選項(xiàng)
選擇類型描述
會期MongoDB\驅(qū)動程序\會話

要與操作關(guān)聯(lián)的會話。

寫關(guān)注點(diǎn)MongoDB\驅(qū)動程序\WriteConcern

要應(yīng)用于操作的寫入關(guān)注點(diǎn)。

返回值 

成功后返回 MongoDB\Driver\WriteResult。

錯誤/異常 

  • 如果不包含任何寫入操作,則拋出 MongoDB\Driver\Exception\InvalidArgumentException。bulk
  • 如果已執(zhí)行,則拋出 MongoDB\Driver\Exception\InvalidArgumentException。MongoDB\Driver\BulkWrite 對象不能多次執(zhí)行。bulk
  • 如果該選項(xiàng)與未確認(rèn)的寫入問題結(jié)合使用,則引發(fā) MongoDB\Driver\Exception\InvalidArgumentException。"session"
  • 在參數(shù)分析錯誤時拋出 MongoDB\Driver\Exception\InvalidArgumentException。
  • 如果與服務(wù)器的連接失?。ㄓ捎谏矸蒡?yàn)證以外的原因),則拋出 MongoDB\Driver\Exception\ConnectionException。
  • 如果需要身份驗(yàn)證并失敗,則拋出 MongoDB\Driver\Exception\AuthenticationException。
  • 在任何寫入失?。ɡ鐚懭脲e誤、無法應(yīng)用寫入問題)時拋出 MongoDB\Driver\Exception\BulkWriteException
  • 在其他錯誤上拋出 MongoDB\Driver\Exception\RuntimeException。

更新日志 

版本說明
PECL mongodb 1.4.4MongoDB\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.0MongoDB\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)

參見 

  • MongoDB\驅(qū)動程序\批量寫入
  • MongoDB\驅(qū)動程序\WriteResult
  • MongoDB\驅(qū)動程序\WriteConcern
  • MongoDB\Driver\Server::executeBulkWrite() - 在此服務(wù)器上執(zhí)行一個或多個寫入操作


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號