# Executing Commands # Listing databases and collections ```php 1]); $result = $manager->executeCommand("admin", $listdatabases); /* The command returns a single result document, which contains the information * for all databases in a "databases" array field. */ $databases = current($result->toArray()); foreach ($databases["databases"] as $database) { echo $database->name, "\n"; // Construct and execute the listCollections command for each database $listcollections = new MongoDB\Driver\Command(["listCollections" => 1]); $result = $manager->executeCommand($database->name, $listcollections); /* The command returns a cursor, which we can iterate on to access * information for each collection. */ $collections = $result->toArray(); foreach ($collections as $collection) { echo "\t * ", $collection->name, "\n"; } } ?> ``` # Create a user ```php "USERNAME2", "pwd" => "PASSWORD", "roles" => [ ["role" => "clusterAdmin", "db" => "admin"], ["role" => "readWriteAnyDatabase", "db" => "admin"], ["role" => "userAdminAnyDatabase", "db" => "admin"], "readWrite", ], "writeConcern" => ["w" => "majority"], ]); try { $result = $manager->executeCommand("admin", $createuser); $response = current($result->toArray()); if ($response["ok"]) { echo "User created successfully\n"; } } catch (MongoDB\Driver\Exception\Exception $e) { echo $e->getMessage(), "\n"; } ?> ``` ## Commands and read preferences ```php "iceland", "datacenter" => "west"], // Fall back to any datacenter in Iceland ["country" => "iceland"], // If Iceland is offline, read from whatever is available [], ] ); // Construct the MongoDB Manager $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); $query = ["citizen" => "Iceland"]; $count = new MongoDB\Driver\Command(["count" => "collection", "query" => $query]); try { $result = $manager->executeCommand("db", $count, $rp); $response = current($result->toArray()); if ($response["ok"]) { printf("db.collection has %d document(s) matching: %s\n", $response["n"], MongoDB\BSON\toJSON(MongoDB\BSON\fromArray($query)) ); } } catch (MongoDB\Driver\Exception\Exception $e) { echo $e->getMessage(), "\n"; } ?> ```