The MongoDB\Driver\Command class

(mongodb >=1.0.0)

Einführung

The MongoDB\Driver\Command class is a value object that represents a database command.

To provide Command Helpers the MongoDB\Driver\Command object should be composed.

Klassenbeschreibung

final class MongoDB\Driver\Command {
/* Methoden */
final public __construct(array|object $document, array $commandOptions = ?)
}

Beispiele

Beispiel #1 Composing MongoDB\Driver\Command to provide a helper to create collections

<?php
class CreateCollection {
    protected 
$cmd = array();

    function 
__construct($collectionName) {
        
$this->cmd["create"] = (string)$collectionName;
    }
    function 
setCappedCollection($maxBytes$maxDocuments false) {
        
$this->cmd["capped"] = true;
        
$this->cmd["size"]   = (int)$maxBytes;

        if (
$maxDocuments) {
            
$this->cmd["max"] = (int)$maxDocuments;
        }
    }
    function 
usePowerOf2Sizes($bool) {
        if (
$bool) {
            
$this->cmd["flags"] = 1;
        } else {
            
$this->cmd["flags"] = 0;
        }
    }
    function 
setFlags($flags) {
        
$this->cmd["flags"] = (int)$flags;
    }
    function 
getCommand() {
        return new 
MongoDB\Driver\Command($this->cmd);
    }
    function 
getCollectionName() {
        return 
$this->cmd["create"];
    }
}


$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$createCollection = new CreateCollection("cappedCollection");
$createCollection->setCappedCollection(64 1024);

try {
    
$command $createCollection->getCommand();
    
$cursor $manager->executeCommand("databaseName"$command);
    
$response $cursor->toArray()[0];
    
var_dump($response);

    
$collstats = ["collstats" => $createCollection->getCollectionName()];
    
$cursor $manager->executeCommand("databaseName", new MongoDB\Driver\Command($collstats));
    
$response $cursor->toArray()[0];
    
var_dump($response);
} catch(
MongoDB\Driver\Exception $e) {
    echo 
$e->getMessage(), "\n";
    exit;
}

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

object(MongoDB\Driver\Command)#3 (1) {
  ["command"]=>
  array(3) {
    ["create"]=>
    string(16) "cappedCollection"
    ["capped"]=>
    bool(true)
    ["size"]=>
    int(65536)
  }
}
array(1) {
  ["ok"]=>
  float(1)
}
array(16) {
  ["ns"]=>
  string(29) "databaseName.cappedCollection"
  ["count"]=>
  int(0)
  ["size"]=>
  int(0)
  ["numExtents"]=>
  int(1)
  ["storageSize"]=>
  int(65536)
  ["nindexes"]=>
  int(1)
  ["lastExtentSize"]=>
  float(65536)
  ["paddingFactor"]=>
  float(1)
  ["paddingFactorNote"]=>
  string(101) "paddingFactor is unused and unmaintained in 2.8. It remains hard coded to 1.0 for compatibility only."
  ["userFlags"]=>
  int(0)
  ["capped"]=>
  bool(true)
  ["max"]=>
  int(9223372036854775807)
  ["maxSize"]=>
  int(65536)
  ["totalIndexSize"]=>
  int(8176)
  ["indexSizes"]=>
  object(stdClass)#4 (1) {
    ["_id_"]=>
    int(8176)
  }
  ["ok"]=>
  float(1)
}

Inhaltsverzeichnis

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

How to wrap text properly?

I'm new to ReadMe, and I've observed that the text doesn't always wrap as I would want when I add images to my documentation. How can I make sure ...

Geschrieben von TorhanPiper am 31.05.2024 09:43:43
Forum: HTML, JavaScript, AJAX, jQuery, CSS, Bootstrap, LESS
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