Altas Test Database Beginner's Terrible Experience

Preformatted textSpent all day wrestling with poor documentation in as of now failed attempt to test MongdoDB for an application. Finally appear to be able to establish a connection with Atlas sample database from external Linux server with latest PHP Library and Extension, yet listCollections call yields empty response.

Complete code:

#Following two lines commented out since call to ServerApi results in error: "Fatal error: Uncaught Error: Class 'ServerApi' not found i$
#$serverApi = new ServerApi(ServerApi::V1);
#$client = new MongoDB\Client('mongodb+srv://username:password@cluster0.irus4oq.mongodb.net/?retryWrites=true&w=majority', [], ['server$
$client = new MongoDB\Client('mongodb+srv://username:password@cluster0.irus4oq.mongodb.net/?retryWrites=true&w=majority');
 
$database = $client->test;
 
$cursor = $database->command(['ping' => 1]);

$cursor = $database->command(['listCollections' => 1]);
foreach ($cursor as $collection) {
    echo $collection['name'], "\n";
}
 
foreach ($database->listCollections() as $collectionInfo) {
    var_dump($collectionInfo);
}

var_dump after $database= $client->test:

[“databaseName”]=>
string(4) “test”
[“manager”]=>
object(MongoDB\Driver\Manager)#2 (2) {
[“uri”]=>
string(88) “mongodb+srv:/username:password@cluster0.irus4oq.mongodb.net/?retryWrites=true&w=majority”
[“cluster”]=>
array(0) {
}
}
[“readConcern”]=>
object(MongoDB\Driver\ReadConcern)#8 (0) {
}
[“readPreference”]=>
object(MongoDB\Driver\ReadPreference)#9 (1) {
[“mode”]=>
string(7) “primary”
}
[“typeMap”]=>
array(3) {
[“array”]=>
string(23) “MongoDB\Model\BSONArray”
[“document”]=>
string(26) “MongoDB\Model\BSONDocument”
[“root”]=>
string(26) “MongoDB\Model\BSONDocument”
}
[“writeConcern”]=>
object(MongoDB\Driver\WriteConcern)#10 (1) {
[“w”]=>
string(8) “majority”
}
}

No collections returned???

May be there is no collection in your database. Can you connect with Compass and share a screenshot of the collections you have?

It looks like there is a discrepancy between the code

and the following output (looks like a missing /)

May be the missing slash was removed while redacting the URI.

You appear to be connecting to your test database. Do you have any collections in that database?

I think your code will probably work if you connect to a database with collections.

Have been able to connect and access now that collectoins are in database.

Still surprised by basic error in documentation. For instance, the documentation describes Mongo Compass on the Mac as being installed in /Applications/MongoDB while it is actually installed as /Applications/MongoDB Compass. Small difference, but confusing to beginners. Why can’t the company keep documentation current and accurate?

Glad you were able to connect!

I’m not quite following this however:

In the documentation it says:

The executable is called MongoDB Compass . The installer installs it under the Applications folder:

/Applications/MongoDB\ Compass.app/Contents/MacOS/MongoDB\ Compass

The app is indeed called “MongoDB Compass” and it is in the /Applications/ folder (as the MongoDB Compass.app) .

MongoDB\ Compass.app == MongoDB Compass.app as the backslash is to escape the space.

Seems like everything is correct?