Skip to content

MongoDB

MongoDBContainer configures credentials and waits until mongosh can execute a command.

Requirements

  • PHP extension: ext-mongodb
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

declare(strict_types=1);

use Testcontainers\Modules\MongoDBContainer;

$container = (new MongoDBContainer())->start();

try {
    $pingResult = $container->exec([
        'mongosh',
        'admin',
        '-u',
        'test',
        '-p',
        'test',
        '--eval',
        '\'db.runCommand("ping").ok\'',
    ]);

    echo $pingResult;
} finally {
    $container->stop();
}