MongoDB\Driver\BulkWrite::__construct

(mongodb >=1.0.0)

MongoDB\Driver\BulkWrite::__constructCreate a new BulkWrite

Beschreibung

public MongoDB\Driver\BulkWrite::__construct(array $options = ?)

Constructs a new MongoDB\Driver\BulkWrite, which is a mutable object to which one or more write operations may be added. The write(s) may then be executed with MongoDB\Driver\Manager::executeBulkWrite().

Parameter-Liste

options (array)

options
Option Type Description Default
bypassDocumentValidation bool

If true, allows insert and update operations to circumvent document level validation.

This option is available in MongoDB 3.2+ and is ignored for older server versions, which do not support document level validation.

false
comment mixed

An arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs.

This option is available in MongoDB 4.4+ and will result in an exception at execution time if specified for an older server version.

let array|object

Map of parameter names and values. Values must be constant or closed expressions that do not reference document fields. Parameters can then be accessed as variables in an aggregate expression context (e.g. $$var).

This option is available in MongoDB 5.0+ and will result in an exception at execution time if specified for an older server version.

ordered bool Ordered operations (true) are executed serially on the MongoDB server, while unordered operations (false) are sent to the server in an arbitrary order and may be executed in parallel. true

Fehler/Exceptions

Changelog

Version Beschreibung
PECL mongodb 1.14.0 Added the "comment" and "let" options.
PECL mongodb 1.1.0 Added the "bypassDocumentValidation" option.

Beispiele

Beispiel #1 MongoDB\Driver\BulkWrite::__construct() example

<?php

$bulk 
= new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->delete([]);
$bulk->insert(['_id' => 1'x' => 1]);
$bulk->insert(['_id' => 2'x' => 2]);
$bulk->update(
    [
'x' => 2],
    [
'$set' => ['x' => 1]],
    [
'limit' => 1'upsert' => false]
);
$bulk->delete(['x' => 1], ['limit' => 1]);
$bulk->update(
    [
'_id' => 3],
    [
'$set' => ['x' => 3]],
    [
'limit' => 1'upsert' => true]
);

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$writeConcern = new MongoDB\Driver\WriteConcern(1);

try {
    
$result $manager->executeBulkWrite('db.collection'$bulk$writeConcern);
} catch (
MongoDB\Driver\Exception\BulkWriteException $e) {
    
$result $e->getWriteResult();

    
// Check if the write concern could not be fulfilled
    
if ($writeConcernError $result->getWriteConcernError()) {
        
printf("%s (%d): %s\n",
            
$writeConcernError->getMessage(),
            
$writeConcernError->getCode(),
            
var_export($writeConcernError->getInfo(), true)
        );
    }

    
// Check if any write operations did not complete at all
    
foreach ($result->getWriteErrors() as $writeError) {
        
printf("Operation#%d: %s (%d)\n",
            
$writeError->getIndex(),
            
$writeError->getMessage(),
            
$writeError->getCode()
        );
    }
} catch (
MongoDB\Driver\Exception\Exception $e) {
    
printf("Other error: %s\n"$e->getMessage());
    exit;
}

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

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Inserted 2 document(s)
Updated  1 document(s)
Upserted 1 document(s)
Deleted  1 document(s)

Siehe auch

Hier Kannst Du einen Kommentar verfassen


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

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

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
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

Berechnungen durchführen

Hallo liebe Forenmitglieder, meine erste frage ist zum Aufbau meiner kleinen Berechnungswebseite, nichts kommerzielles, soll nur eine Anwendung f ...

Geschrieben von matze511 am 21.04.2024 21:42:37
Forum: PHP Developer Forum
Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche
Wir stellen unsere SEO-Agentur vor

Hallo In der heutigen digitalen Welt war es für Unternehmen noch nie so einfach, ihre Reichweite weltweit zu vergrößern. Wenn Sie außerhalb I ...

Geschrieben von thomasmuller am 14.04.2024 07:18:33
Forum: User stellen sich vor
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hallöchen zusammen, ich versuche derzeit unseren Excel-Spielplan in PHP zu überführen. Eigentlich bin ich auch shon fertig - wenn da nicht dies ...

Geschrieben von derH0st am 11.04.2024 15:58:37
Forum: PHP Developer Forum