We have an external mongodb instance and we need to query data from it and bring it to Drupal 8 site.

On shell this is how we connect

mongo --host docker_mongo --ssl --sslCAFile=/etc/ssl/mongodb-cert.crt 

Could anyone suggest how to connect to a mongodb instance?
Adding the following at settings.php does not work and status report shows "No database aliases found in settings. Did you actually configure your settings ?"

$configureMongoDb = function (array $settings): array {
  $settings['mongodb'] = [
    'clients' => [
      // Client alias => connection constructor parameters.
      'mymongo' => [
        'uri' => 'mongodb://docker_mongo:27017',
        'uriOptions' => ['ssl=true&sslCAFile=/etc/ssl/mongodb-cert.crt'],
        'driverOptions' => [],
      ],
    ],
    'databases' => [
      // Database alias => [ client_alias, database_name ].
      'tutorial2' => ['mymongo', 'tutorial2'],
    ],
  ];

  return $settings;
};

Cross posted this query on drupal.stackexchange as well.

Comments

toamit created an issue. See original summary.

toamit’s picture

Issue summary: View changes
toamit’s picture

Issue summary: View changes
fgm’s picture

The fragment you show defines a closure performing the configuration, and seems correct.

But did you call it further down in your settings.php file ? Because if you don't it's just a function and doesn't do anything until you call it like:

$settings = $configureMongodb($settings);
fgm’s picture

Status: Active » Postponed (maintainer needs more info)
fgm’s picture

Version: 8.x-1.x-dev » 8.x-2.0

Fixing version: 8.x-1.x is not supported, and this style of settings belongs to the 8.x-2.x branch.

toamit’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

Thanks, I had another issue as pecl install mongodb was not installing ssl which caused failure to connect with mongoldb

fgm’s picture

Thanks for closing the issue. Do you feel the doc or code could have helped you find the issue faster ? and if so how ?

toamit’s picture

@fgm I was hoping to provide documentable information, but could not provide it, so atleast mention it here.
It would be nice include the following in readme and documentation.

  • Exact function call after db configuration in settings.php, this is what I missed

Another convenience that the module could provide is to add a button say "Test connection" in module configuration, this way we don't have to sieve through status report. The button would display success or error message in the UI.

Thanks for the module and being thoughtful on how documentation can be improved, which is quite rare in contributed or even core space.

fgm’s picture

Thanks for the suggestion, follow-up issue added as #3023469: Add a configuration check in the UI.