Repository: opsdroid/opsdroid (language: python)

Issue:
User configurable connection for mongo-based databases
So the pymongo client has a multitude of ways for connecting to different mongo services

So for MongoDB Atlas users the connection string is given as such 
for python connections to the mongo db atlas 

`mongodb+srv://<username>:<password>@<cluster-name>.mongodb.net/myFirstDatabase`

In making the mongo connection to be user configurable we can specify different types of mongo services versus
just asking for the basic connection arguments like port, user name,  pass, and also we can give users an easier way to connect versus making assumptions about the type of mongodb the kinds of credentials they might have. 

As long as the pymongo client accepts the connection and connects the user to the database and the collection they want I think this would be great!

Thanks again guys!



Discussion / hints:

Fix patch:
diff --git a/docs/databases/mongo.md b/docs/databases/mongo.md
index 350c8d0e9..2264ffe7e 100644
--- a/docs/databases/mongo.md
+++ b/docs/databases/mongo.md
@@ -11,12 +11,13 @@ An accessible MongoDB server with the database that you provide already created.
 ```yaml
 databases:
   mongo:
-    host:                       "my_host"       # (optional) default "localhost"
-    port:                       "12345"         # (optional) default "27017"
-    database:                   "my_database"   # (optional) default "opsdroid"
-    collection:                 "my_collection" # (optional) default "opsdroid"
-    user:                       "my_user"       # (optional)
-    password:                   "pwd123!"       # (optional)
+    host:                       "my_host"         # (optional) default "localhost"
+    port:                       "12345"           # (optional) default "27017"
+    database:                   "my_database"     # (optional) default "opsdroid"
+    protocol:                   "mongodb://"      # (optional) default "mongodb://"
+    collection:                 "my_collection"   # (optional) default "opsdroid"
+    user:                       "my_user"         # (optional)
+    password:                   "pwd123!"         # (optional)
 ```
 
 ## Usage
diff --git a/opsdroid/database/mongo/__init__.py b/opsdroid/database/mongo/__init__.py
index 30f6134b3..f6df5c5b0 100644
--- a/opsdroid/database/mongo/__init__.py
+++ b/opsdroid/database/mongo/__init__.py
@@ -44,17 +44,18 @@ def __init__(self, config, opsdroid=None):
     async def connect(self):
         """Connect to the database."""
         host = self.config.get("host", "localhost")
+        protocol = self.config.get("protocol", "mongodb").replace("://", "")
         port = self.config.get("port", "27017")
+        if port != "27017":
+            host = f"{host}:{port}"
         database = self.config.get("database", "opsdroid")
         user = self.config.get("user")
         pwd = self.config.get("password")
         if user and pwd:
-            path = "mongodb://{user}:{pwd}@{host}:{port}".format(
-                user=user, pwd=pwd, host=host, port=port
-            )
+            self.db_url = f"{protocol}://{user}:{pwd}@{host}"
         else:
-            path = "mongodb://{host}:{port}".format(host=host, port=port)
-        self.client = AsyncIOMotorClient(path)
+            self.db_url = f"{protocol}://{host}"
+        self.client = AsyncIOMotorClient(self.db_url)
         self.database = self.client[database]
         _LOGGER.info("Connected to MongoDB.")
 
diff --git a/setup.cfg b/setup.cfg
index 70a4faa21..cd50e132d 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -117,6 +117,7 @@ database_sqlite =
   aiosqlite>=0.15.0
 database_mongo =
   motor>=2.1.0
+  dnspython>=2.1.0
 database_matrix =
   wrapt>=1.12.1
 # testing


Test patch:
diff --git a/opsdroid/database/mongo/tests/test_mongo.py b/opsdroid/database/mongo/tests/test_mongo.py
index d57414020..55c3a6883 100644
--- a/opsdroid/database/mongo/tests/test_mongo.py
+++ b/opsdroid/database/mongo/tests/test_mongo.py
@@ -44,6 +44,8 @@ def test_init(database):
         {
             "database": "test_db",
             "collection": "test_collection",
+            "protocol": "mongodb://",
+            "port": "1234",
             "user": "root",
             "password": "mongo",
         },
@@ -53,6 +55,7 @@ async def test_connect(database):
     """Test that the mongo database has implemented connect function properly"""
     try:
         await database.connect()
+        assert "mongodb://" in database.db_url
     except NotImplementedError:
         raise Exception
     else: