Skip to content

Command mongo #14

Open
Open

Description

@sktstebo

Hi,

The new version mongoDB 6.x doesn't provide "mongo" command, there is a new command "mongosh".

https://www.mongodb.com/docs/mongodb-shell/
https://www.mongodb.com/blog/post/introducing-the-new-shell

Can you update your shell command in /Driver/MongoDb.php ?

Thanks!

Activity

  1. Naktibalda commented on May 6, 2023

    @Naktibalda
    Member

    If you need it, please implement it and raise pull request.

    This module already supports multiple dump loading methods, so adding another one should be trivial.

    1. Implement and document new dump_type value:
      * * dump_type *required* - type of dump.
      * One of 'js' (MongoDb::DUMP_TYPE_JS), 'mongodump' (MongoDb::DUMP_TYPE_MONGODUMP) or 'mongodump-tar-gz' (MongoDb::DUMP_TYPE_MONGODUMP_TAR_GZ).
      * default: MongoDb::DUMP_TYPE_JS).
      * * dump - path to database dump
      * * populate: true - should the dump be loaded before test suite is started.
      * * cleanup: true - should the dump be reloaded after each test.
      * Boolean or 'dirty'. If cleanup is set to 'dirty', the dump is only reloaded if any data has been written to the db during a test. This is
      * checked using the [dbHash](https://docs.mongodb.com/manual/reference/command/dbHash/) command.
      *
      */
      class MongoDb extends Module
      {
      /**
      * @var string
      */
      public const DUMP_TYPE_JS = 'js';
      /**
      * @var string
      */
      public const DUMP_TYPE_MONGODUMP = 'mongodump';
      /**
      * @var string
      */
      public const DUMP_TYPE_MONGODUMP_TAR_GZ = 'mongodump-tar-gz';

    I think that it should be named mongosh:

    public const DUMP_TYPE_MONGOSH = 'mongosh';
    1. Implement handling of new type in validateDump and loadDump.
      (If it handles the same dump files as js type, you can add new condition to if ($this->config['dump_type'] === self::DUMP_TYPE_JS) { in validation code).

      if ($this->config['dump_type'] === self::DUMP_TYPE_JS) {
      $content = file_get_contents($this->dumpFile);
      $content = trim(preg_replace('#/\*(?:(?!\*/).)*\*/#s', "", $content));
      if (count(explode("\n", $content)) === 0) {
      $this->isDumpFileEmpty = true;
      }
      return;
      }

    2. Implement loadUsingMongoSh method in Driver file:

      public function load(string $dumpFile): void
      {
      $cmd = sprintf(
      'mongo %s %s%s',
      $this->host . '/' . $this->dbName,
      $this->createUserPasswordCmdString(),
      escapeshellarg($dumpFile)
      );
      shell_exec($cmd);
      }
      public function loadFromMongoDump(string $dumpFile): void
      {
      [$host, $port] = $this->getHostPort();
      $cmd = sprintf(
      "mongorestore %s --host %s --port %s -d %s %s %s",
      $this->quiet,
      $host,
      $port,
      $this->dbName,
      $this->createUserPasswordCmdString(),
      escapeshellarg($dumpFile)
      );
      shell_exec($cmd);
      }
      public function loadFromTarGzMongoDump(string $dumpFile): void
      {
      [$host, $port] = $this->getHostPort();
      $getDirCmd = sprintf(
      "tar -tf %s | awk 'BEGIN { FS = \"/\" } ; { print $1 }' | uniq",
      escapeshellarg($dumpFile)
      );
      $dirCountCmd = $getDirCmd . ' | wc -l';
      if (trim(shell_exec($dirCountCmd)) !== '1') {
      throw new ModuleException(
      $this,
      'Archive MUST contain single directory with db dump'
      );
      }
      $dirName = trim(shell_exec($getDirCmd));
      $cmd = sprintf(
      'tar -xzf %s && mongorestore %s --host %s --port %s -d %s %s %s && rm -r %s',
      escapeshellarg($dumpFile),
      $this->quiet,
      $host,
      $port,
      $this->dbName,
      $this->createUserPasswordCmdString(),
      $dirName,
      $dirName
      );
      shell_exec($cmd);
      }

  2. sktstebo commented on May 9, 2023

    @sktstebo
    Author

    Ok, i understand but why implements a new type ?
    "mongosh" is the remplacement from "mongo". When you started mongo command (version 5.x), there is a warning deprecated message.

    So, why don't just implements as like :

    $cmd = sprintf( 
             'mongosh ......'
         ); 
    $resultCode = null;
    exec($cmd, null, $resultCode);
    if ($resultCode == '127') { // i believe than 127 error level is equivalent to "Command not found"
      // try with the old command
      $cmd = sprintf( 
               'mongo %s %s%s', 
               $this->host . '/' . $this->dbName, 
               $this->createUserPasswordCmdString(), 
               escapeshellarg($dumpFile) 
           ); 
          $exitErrorLevel =  exec($cmd); 
    }
    ....
    ....
    
  3. Naktibalda commented on May 20, 2023

    @Naktibalda
    Member

    but why implement a new type ?

    Because it is simpler to implement :)

    Your idea is good, but I think that executing mongosh with fallback to mongo should be released as a new major version (4.0.0), to avoid executing failing command on every run for all users that are currently using it with mongo.

    Executing mongo first with fallback to mongosh could be released as a minor version - 3.1.0

  4. W0rma commented on Jan 9, 2026

    @W0rma
    Contributor

    Fixed in #18

    @TavoNiievez This issue can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions