Skip to content

Commit 183286a

Browse files
committed
Add an example "document browser"
1 parent 89cd4fa commit 183286a

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ fast and powerful.
2727

2828
### Examples
2929

30-
* [Model classes](docs/examples/model.php) (referenced in other examples)
31-
* [Querying](docs/examples/query.php)
32-
* [Inserting](docs/examples/insert.php)
33-
* Custom [BSON deserialization](docs/examples/changing-types.php)
30+
* [List documents in collections & collections in database](docs/examples/list-documents-in-collections.php.php)
3431

3532
## Installation
3633

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
$manager = new MongoDB\Manager("mongodb://localhost:27017");
4+
5+
6+
/* List all available commands */
7+
8+
$listcommands = new MongoDB\Command(array("listCommands" => 1));
9+
$retval = $manager->executeCommand("admin", $listcommands);
10+
$commands = $retval->getResponseDocument();
11+
12+
foreach($commands["commands"] as $command => $info) {
13+
echo $command, "\n -- ", trim(explode("\n", $info->help)[0]), "\n";
14+
}
15+
16+
17+
echo "\n\n\n";
18+
19+
20+
/* List all Databases */
21+
$listdbs = new MongoDB\Command(array("listDatabases" => 1));
22+
$retval = $manager->executeCommand("admin", $listdbs);
23+
$dbs = $retval->getResponseDocument();
24+
25+
foreach($dbs["databases"] as $dbinfo) {
26+
/* Print the Database name and its size on disk */
27+
echo $dbinfo->name, " (", $dbinfo->sizeOnDisk / 1024 / 1024, "mb)\n";
28+
29+
/* List all the Collections within that database */
30+
$listcollections = new MongoDB\Command(array("listCollections" => 1));
31+
$retval = $manager->executeCommand($dbinfo->name, $listcollections);
32+
$collections = $retval->getResponseDocument();
33+
34+
foreach($collections["collections"] as $collinfo) {
35+
/* Retreieve the collection statistics for this collection */
36+
$collstats = new MongoDB\Command(array("collStats" => $collinfo->name));
37+
$retval = $manager->executeCommand($dbinfo->name, $collstats);
38+
$stats = $retval->getResponseDocument();
39+
40+
/* Print the collection name and document count */
41+
echo " - ", $collinfo->name, " (" , $stats["count"], " document(s))\n";
42+
43+
/* Fetch all documents in this collection, but only retrieve the _id field */
44+
$query = new MongoDB\Query(array(), array("projection" => array("_id" => 1)));
45+
$qi = $manager->executeQuery($dbinfo->name . "." . $collinfo->name, $query);
46+
foreach($qi as $document) {
47+
/* Not all documents have _id field (special internal collections for example) */
48+
if (isset($document["_id"])) {
49+
echo " - _id: ", $document["_id"], "\n";
50+
} else {
51+
echo " - (noid)\n";
52+
}
53+
}
54+
}
55+
}
56+
/* The total size of all the databases */
57+
echo "--\nTotal Size: ", $dbs["totalSize"] / 1024 / 1024, "mb\n";
58+
59+
60+

0 commit comments

Comments
 (0)