PHP 5.6.0 released

MongoDB::listCollections

(PECL mongo >=0.9.0)

MongoDB::listCollectionsGets an array of all MongoCollections for this database

Description

public array MongoDB::listCollections ([ bool $includeSystemCollections = false ] )

Gets a list of all the collections in the database and returns them as an array of MongoCollection objects.

Parameters

includeSystemCollections

Include system collections.

Return Values

Returns an array of MongoCollection objects.

Changelog

Version Description
1.3.0 Added the includeSystemCollections parameter.

Examples

Example #1 MongoDB::listCollections() example

The following example demonstrates dropping each collection in a database.

<?php

$m 
= new MongoClient();
$db $m->selectDB("sample");

$list $db->listCollections();
foreach (
$list as $collection) {
    echo 
"removing $collection... ";
    
$collection->drop();
    echo 
"gone\n";
}

?>

The above example will output something similar to:

removing sample.blog.posts... gone
removing sample.critical.docs... gone
removing sample.taxes... gone
...

See Also

add a note add a note

User Contributed Notes 3 notes

up
1
Anonymous
14 days ago
Before using the example given by PHP.. Read what it does... whoever wrote this example needs to rethink that one.
up
0
George Gombay
2 months ago
For an alternative to the shell command 'show dbs', refer to my note for the listDBs() method of Mongoclient(), which reproduces some simple PHP code that will yield the names of all database present.
up
0
Matt Saunders
4 years ago
Currently, the PHP equivalent to "show dbs" is:

$db->command(array("listDatabases" => 1));

According to kristina1 in #mongodb, there will be a proper helper (listDatabases() I presume ) for this command in a later version.
To Top