Skip to content

Commit 3b435ee

Browse files
authored
change auth style for mongodb to simplify it while still leaving ability to do other auth styles (#854)
1 parent a0fb56b commit 3b435ee

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

docs/connectors/sinks/mongodb-sink.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ this `product_category` ("Shirts") and update them to have these new `color_opti
190190

191191
```python
192192
mongodb_sink = MongoDBSink(
193-
url="mongodb://localhost:27017",
193+
host="localhost",
194194
db="my_mongodb",
195195
collection="clothing",
196196

@@ -204,9 +204,12 @@ mongodb_sink = MongoDBSink(
204204

205205
## Configuration Options
206206

207-
- `url`: MongoDB url; most commonly `mongodb://username:password@host:port`
207+
- `host`: MongoDB hostname; example "localhost"
208208
- `db`: MongoDB database name
209209
- `collection`: MongoDB collection name
210+
- `username`: username, if authentication is required
211+
- `password`: password, if authentication is required
212+
- `port`: port used by MongoDB host if not using the default of `27017`
210213
- `document_matcher`: How documents are selected to update.
211214
A callable that accepts a `BatchItem` and returns a MongoDB "Filter Query".
212215
If no match, will insert if `upsert=True`, where `_id` will be either the
@@ -257,5 +260,11 @@ You can test your application using a local MongoDB host via Docker:
257260
mongodb/mongodb-community-server:latest
258261
```
259262

260-
2. Connect using the url:
261-
`mongodb://localhost:27017`
263+
2. Connect using `localhost` for the `host` argument to `MongoDBSink`:
264+
265+
```python
266+
MongoDBSink(
267+
host="localhost",
268+
# other required args here...
269+
)
270+
```

quixstreams/sinks/community/mongodb.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import uuid
77
from functools import partial
88
from typing import Callable, Literal, Optional, Union
9+
from urllib.parse import quote_plus as qp
910

1011
import bson
1112

@@ -65,9 +66,12 @@ def _default_document_matcher(record: SinkItem) -> MongoQueryFilter:
6566
class MongoDBSink(BatchingSink):
6667
def __init__(
6768
self,
68-
url: str,
69+
host: str,
6970
db: str,
7071
collection: str,
72+
username: Optional[str] = None,
73+
password: Optional[str] = None,
74+
port: int = 27017,
7175
document_matcher: Callable[
7276
[SinkItem], MongoQueryFilter
7377
] = _default_document_matcher,
@@ -82,9 +86,12 @@ def __init__(
8286
"""
8387
A connector to sink processed data to MongoDB in batches.
8488
85-
:param url: MongoDB url; most commonly `mongodb://username:password@host:port`
89+
:param host: MongoDB hostname; example "localhost"
8690
:param db: MongoDB database name
8791
:param collection: MongoDB collection name
92+
:param username: username, if authentication is required
93+
:param password: password, if authentication is required
94+
:param port: port used by MongoDB host if not using the default of 27017
8895
:param document_matcher: How documents are selected to update.
8996
A callable that accepts a `BatchItem` and returns a MongoDB "query filter".
9097
If no match, will insert if `upsert=True`, where `_id` will be either the
@@ -107,9 +114,13 @@ def __init__(
107114
NOTE: metadata is added before this step, so don't accidentally
108115
exclude it here!
109116
"""
110-
111117
super().__init__()
112-
self._url = url
118+
auth_stub = f"{qp(username)}:{qp(password)}@" if username else ""
119+
self._client_kwargs = {
120+
"host": f"mongodb://{auth_stub}{host}",
121+
"port": port,
122+
**kwargs,
123+
}
113124
self._db_name = db
114125
self._collection_name = collection
115126
self._document_matcher = document_matcher
@@ -119,13 +130,11 @@ def __init__(
119130
self._add_topic_metadata = add_topic_metadata
120131
self._value_selector = value_selector
121132
self._auth_timeout_ms = authentication_timeout_ms
122-
self._client_kwargs = kwargs
123133
self._client: Optional[MongoClient] = None
124134
self._collection: Optional[Collection] = None
125135

126136
def setup(self):
127137
self._client = MongoClient(
128-
self._url,
129138
serverSelectionTimeoutMS=self._auth_timeout_ms,
130139
**self._client_kwargs,
131140
)

0 commit comments

Comments
 (0)