Configure connection to mongodb for querying only
You don't need to set a function in settings.php to define the connection. The documentation lists this as an example:

https://fgm.github.io/mongodb/install/#settings-configuration

// In sites/default/settings.local.php.
$settings['mongodb'] = [
  'clients' =&gt; [
    // Client alias =&gt; connection constructor parameters.
    'default' =&gt; [
      'uri' =&gt; 'mongodb://localhost:27017',
      'uriOptions' =&gt; [],
      'driverOptions' =&gt; [],
    ],
  ],
  'databases' =&gt; [
    // Database alias =&gt; [ client_alias, database_name ]
    'default' =&gt; ['default', 'drupal'],
    'keyvalue' =&gt; ['default', 'keyvalue'],
    'logger' =&gt; ['default', 'logger'],
  ],
];