1

Here's my db:

$db = new Mongo("mongodb://u:[email protected]:37068/dbname");

I want to echo a list/array of collections (the names) on the database.

How can I do that? Thanks.

1 Answer 1

4

You can use listCollections: http://php.net/manual/en/mongodb.listcollections.php

<?php

$db = new Mongo("mongodb://u:[email protected]:37068/dbname");
$list = $db->listCollections();
foreach ($list as $collection) {
    echo "$collection \n";
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

I mean i need to iterate through these. When I tried echo json_encode( $list ); it gave me an array of timeout errors.
$list is an array of MongoCollection which json_encode can't serialize. You need to iterate through them and call $collection->getName() on each MongoCollection and store those results in a new array and call json_encode on the new array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.