From b017d3091599b86a841c6f4f11d0e110bfb45207 Mon Sep 17 00:00:00 2001
From: Gerhard Gonter <ggonter@gmail.com>
Date: Wed, 26 Jul 2023 15:30:12 +0200
Subject: [PATCH] added config options db_auth and db_work to more clearly
 specify which db should be used for authentication and which should be used
 to actually work with

---
 modules/util/Util/MongoDB.pm | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/modules/util/Util/MongoDB.pm b/modules/util/Util/MongoDB.pm
index 16425b9..af409ad 100644
--- a/modules/util/Util/MongoDB.pm
+++ b/modules/util/Util/MongoDB.pm
@@ -102,10 +102,22 @@ $config is a hash which provides the following keys:
  * db_name: Name of the MongoDB
  * username
  * password
+ * collection
 
 optionally:
  * port can currently not be specified
 
+Note about configuration names for the database:
+
+This utility function uses the first entry from the list ('db_name',
+'db_auth', 'db_work', 'db', 'database') as 'db_name' to connect to MongoDB
+and authenticate against it.  If there is a config parameter 'db_work',
+then it's value is use to connect to, otherwise the database which was
+used for authentication is used.
+
+If collection is provided as a config parameter, then a get_collection()
+is done on it's value and the result is returned as second parameter.
+
 =cut
 
 sub connect
@@ -115,7 +127,7 @@ sub connect
 
   my %c;
   foreach my $k1 (['host', 'hostname'], 'port', ['username', 'user'], ['password', 'pass'],
-                 ['db_name', 'db', 'database'], 'collection')
+                 ['db_name', 'db_auth', 'db_work', 'db', 'database'], 'collection')
   {
     if ($k1 eq 'collection')
     {
@@ -184,7 +196,9 @@ DIE:
 =end comment
 =cut
 
-  my $db= $m->get_database ($c{'db_name'});
+  my $db_get= (exists ($mdb->{db_work})) ? $mdb->{db_work} : $c{db_name};
+  # print __LINE__, " db_get=[$db_get]\n";
+  my $db= $m->get_database ($db_get);
   goto DIE unless (defined ($db));
   # print "db=[$db] ", Dumper ($db);
 
-- 
GitLab