Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Development
===========
- (Fill this out as you fix issues and develop your features).
- Fix for uuidRepresentation not read when provided in URI #2741
- Fix connection for multiple db in same cluster created duplicate MongoClient #2768
- Add option to user array_filters https://www.mongodb.com/docs/manual/reference/operator/update/positional-filtered/ #2769
- Fix combination of __raw__ and mongoengine syntax #2773
- Add tests against MongoDB 6.0 and MongoDB 7.0 in the pipeline
Expand All @@ -22,6 +23,7 @@ Development
- Add `text_score` argument on :meth:`~mongoengine.Document.search_text()` to allow text_score computation to be turned off
as it interfer with natural returned documents order #2759


Changes in 0.27.0
=================
- Update uuidRepresentation warnings with "unspecified" as the future default (instead of 'standard' previously advertised) #2739
Expand Down
8 changes: 7 additions & 1 deletion mongoengine/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,13 @@ def _clean_settings(settings_dict):
# Only remove the name but it's important to
# keep the username/password/authentication_source/authentication_mechanism
# to identify if the connection could be shared (cfr https://github.com/MongoEngine/mongoengine/issues/2047)
return {k: v for k, v in settings_dict.items() if k != "name"}
_clean = {k: v for k, v in settings_dict.items() if k != "name"}

# In case of multiple db on the same cluster
_clean["host"] = [
i.replace(f'/{settings_dict["name"]}', "") for i in _clean["host"]
]
return _clean

cleaned_conn_settings = _clean_settings(connection_settings)
for db_alias, connection_settings in connection_settings_bis:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class History2(Document):
connect("db1", alias="db1")
connect("db2", alias="db2")

conn1 = get_connection("db2")
conn2 = get_connection("db2")
assert conn1 == conn2

History.drop_collection()
History1.drop_collection()
History2.drop_collection()
Expand Down Expand Up @@ -704,6 +708,21 @@ def test_connect_uri_uuidrepresentation_default_to_pythonlegacy(self):
)
disconnect(rand)

def test_connect_deduplication(self):
connect(host="mongodb://localhost:27017/mongoenginetest_1", alias="test_1")
connect(host="mongodb://localhost:27017/mongoenginetest_2", alias="test_2")

conn1 = get_connection("test_1")
conn2 = get_connection("test_2")
assert conn1 == conn2
assert isinstance(conn1, pymongo.mongo_client.MongoClient)

db1 = get_db("test_1")
db2 = get_db("test_2")
assert isinstance(db2, pymongo.database.Database)
assert db1.name == "mongoenginetest_1"
assert db2.name == "mongoenginetest_2"


if __name__ == "__main__":
unittest.main()