Currently MongoEngine library only support a single database connection, managing collections by Document classes in global scope.
It has a support to change database connections by switch_db
but it's made by a global class attribute called ._meta['db_alias']
When you define many sub ReferenceFields in a hierarchical tree, only first Document object has its ._meta['db_alias']
atttibute changed, not working to query Documents that implement other documents.
Implementing MongoEngine lib for multi database connections support hierarchical tree of many sub ReferenceFields
$ python execute_poc.py
[db connection: first-db] Account (selecting null values): {}
[db connection: first-db] Phone (selecting null values): {}
[db connection: first-db] Chat (selecting null values): {}
---
[db connection: second-db] Account (selecting null values): {}
[db connection: second-db] Phone (selecting null values): {}
[db connection: second-db] Chat (selecting null values): {}
---
[db connection: second-db] Account (created new value): {"_id": {"$oid": "6616c9862dff85db3d3c90a8"}, "name": "account 1"}
[db connection: second-db] Phone (created new value): {"_id": {"$oid": "6616c9862dff85db3d3c90a9"}, "number": "11 98765-4321", "account": {"$oid": "6616c9862dff85db3d3c90a8"}}
[db connection: second-db] Chat (created new value): {"_id": {"$oid": "6616c9862dff85db3d3c90aa"}, "name": "my first chat", "phone": {"$oid": "6616c9862dff85db3d3c90a9"}}
---
[db connection: second-db] Account (selecting all values): ['{"_id": {"$oid": "6616c9862dff85db3d3c90a8"}, "name": "account 1"}']
[db connection: second-db] Phone (selecting all values): ['{"_id": {"$oid": "6616c9862dff85db3d3c90a9"}, "number": "11 98765-4321", "account": {"$oid": "6616c9862dff85db3d3c90a8"}}']
[db connection: second-db] Chat (selecting all values): ['{"_id": {"$oid": "6616c9862dff85db3d3c90aa"}, "name": "my first chat", "phone": {"$oid": "6616c9862dff85db3d3c90a9"}}']
---
[db connection: second-db] Account (selecting cross values): {"_id": {"$oid": "6616c9862dff85db3d3c90a8"}, "name": "account 1"}
[db connection: second-db] Phone (selecting cross values): {"_id": {"$oid": "6616c9862dff85db3d3c90a9"}, "number": "11 98765-4321", "account": {"$oid": "6616c9862dff85db3d3c90a8"}}
[db connection: second-db] Chat (selecting cross values): {"_id": {"$oid": "6616c9862dff85db3d3c90aa"}, "name": "my first chat", "phone": {"$oid": "6616c9862dff85db3d3c90a9"}}
---
[db connection: first-db] Account (selecting null values again): {}
[db connection: first-db] Phone (selecting null values again): {}
[db connection: first-db] Chat (selecting null values again): {}
---