Skip to content

Commit 4ebbff6

Browse files
committed
Add command docs
1 parent 56aae06 commit 4ebbff6

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

docs/commands.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Running Commands
2+
3+
# Listing databases & collections
4+
5+
```php
6+
<?php
7+
8+
/* Construct the MongoDB Manager */
9+
$manager = new MongoDB\Manager("mongodb://localhost:27017");
10+
11+
12+
$listdatabases = new MongoDB\Command(array("listDatabases" => 1));
13+
$retval = $manager->executeCommand("admin", $listdatabases);
14+
$databases = $retval->getResponseDocument();
15+
16+
foreach($databases["databases"] as $database) {
17+
echo $database->name, "\n";
18+
19+
$listcollections = new MongoDB\Command(array("listCollections" => 1));
20+
$retval = $manager->executeCommand($database->name, $listcollections);
21+
$collections = $retval->getResponseDocument();
22+
foreach($collections["collections"] as $collection) {
23+
echo "\t- ", $collection->name, "\n";
24+
}
25+
}
26+
27+
?>
28+
```
29+
30+
# Create a User
31+
32+
```php
33+
<?php
34+
35+
36+
/* Construct the MongoDB Manager */
37+
$manager = new MongoDB\Manager("mongodb://localhost:27017");
38+
39+
40+
$command = array(
41+
"createUser" => "USERNAME2",
42+
"pwd" => "PASSWORD",
43+
"roles" => array(
44+
array("role" => "clusterAdmin", "db" => "admin"),
45+
array("role" => "readWriteAnyDatabase", "db" => "admin"),
46+
array("role" => "userAdminAnyDatabase", "db" => "admin"),
47+
"readWrite",
48+
),
49+
"writeConcern" => array("w" => "majority"),
50+
);
51+
$createuser = new MongoDB\Command($command);
52+
53+
try {
54+
$result = $manager->executeCommand("admin", $createuser);
55+
$response = $result->getResponseDocument();
56+
if ($reponse["ok"]) {
57+
echo "User created\n";
58+
}
59+
} catch(Exception $e) {
60+
echo $e->getMessage(), "\n";
61+
}
62+
63+
64+
?>
65+
```
66+
67+
## Commands and ReadPreferences
68+
69+
```php
70+
<?php
71+
72+
/* Some commands, like count, dbStats, aggregate, ... can be executed on secondaries.
73+
* Just like with normal queries, an instance of MongoDB\ReadPreference needs to
74+
* be constructed to prefer certain servers over others */
75+
$prefer = MongoDB\ReadPreference::RP_SECONDARY_PREFERRED;
76+
$tags = array(
77+
/* Prefer the West Coast datacenter in Iceland */
78+
array("country" => "iceland", "datacenter" => "west"),
79+
80+
/* Fallback to any datacenter in Iceland */
81+
array("country" => "iceland"),
82+
83+
/* If Iceland is offline, read from whatever is online! */
84+
array(),
85+
);
86+
87+
/* Construct the ReadPreference object from our options */
88+
$rp = new MongoDB\ReadPreference($prefer, $tags);
89+
90+
/* Construct the MongoDB Manager */
91+
$manager = new MongoDB\Manager("mongodb://localhost:27017");
92+
93+
$query = array("citizen" => "Iceland");
94+
$count = new MongoDB\Command(array("count" => "collection", "query" => $query));
95+
$retval = $manager->executeCommand("db", $count, $rp);
96+
$response = $retval->getResponseDocument();
97+
if ($response["ok"]) {
98+
printf("db.collection has %d documents matching: %s\n",
99+
$response["n"],
100+
BSON\toJSON(BSON\fromArray($query))
101+
);
102+
}
103+
104+
?>
105+
```

0 commit comments

Comments
 (0)