MongoDB\Driver\Manager::executeBulkWrite

(mongodb >=1.0.0)

MongoDB\Driver\Manager::executeBulkWriteExecute one or more write operations

Beschreibung

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

Executes one or more write operations on the primary server.

A MongoDB\Driver\BulkWrite can be constructed with one or more write operations of varying types (e.g. updates, deletes, and inserts). The driver will attempt to send operations of the same type to the server in as few requests as possible to optimize round trips.

Parameter-Liste

namespace (String)

A fully qualified namespace (e.g. "databaseName.collectionName").

bulk (MongoDB\Driver\BulkWrite)

The write(s) to execute.

options

options
Option Type Description
session MongoDB\Driver\Session

A session to associate with the operation.

writeConcern MongoDB\Driver\WriteConcern

A write concern to apply to the operation.

Rückgabewerte

Returns MongoDB\Driver\WriteResult on success.

Fehler/Exceptions

Changelog

Version Beschreibung
PECL mongodb 1.4.4 MongoDB\Driver\Exception\InvalidArgumentException will be thrown if the "session" option is used in combination with an unacknowledged write concern.
PECL mongodb 1.4.0 The third parameter is now an options array. For backwards compatibility, this paramater will still accept a MongoDB\Driver\WriteConcern object.
PECL mongodb 1.3.0 MongoDB\Driver\Exception\InvalidArgumentException is now thrown if bulk does not contain any write operations. Previously, a MongoDB\Driver\Exception\BulkWriteException was thrown.

Beispiele

Beispiel #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::MAJORITY100);
$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());
}
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

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)

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Finden Sie die Bestellungen des letzten Monats und die Gesamtausgaben

Ich möchte alle Kunden finden, die im letzten Monat eine Bestellung aufgegeben haben, und den Gesamtbetrag, den jeder Kunde ausgegeben hat. Wenn ...

Geschrieben von Gast am 28.05.2024 11:54:53
Forum: SQL / Datenbanken
PHP cURL Error

Hello I am facing an issue with cURL in my PHP application. When trying to make a secure HTTPS request, I am getting the following error: cURL e ...

Geschrieben von nolanmaris am 27.05.2024 18:27:56
Forum: PHP Developer Forum
Seltsames Verhalten von execute() oder Fehler meinerseits

Hello You can check below steps - Check for errors after executing the statement. Bind parameters to placeholders in the SQL query. Confirm t ...

Geschrieben von nolanmaris am 20.05.2024 12:12:16
Forum: SQL / Datenbanken
Ein Problem mit der Lampe

Yes ..i was facing the same issue .

Geschrieben von nolanmaris am 20.05.2024 12:02:55
Forum: Off-Topic Diskussionen