-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
Minor Change
Currently, we don't consider the values of configurable options when checking equality between instances of MongoClient, Database and Collection. Instead, the equality check only considers:
- the address tuple in the case of MongoClient
- self._client and self._name in the case of Database
- self._database and self._name in the case of Collection
For PyMongo 4.0, we might want to expand these checks to also compare :
- the connection string (and/or uri option kwargs) in the case of MongoClient
- the read_concern, write_concern, read_preference and codec_options in the case of Database
- the read_concern, write_concern, read_preference and codec_options in the case of Collection
In addition to changing the behavior of _eq checks, this change would also modify the hash_ implementations of the respective classes since classes that compare equal must also have the same hash value. This would allow users to hash multiple objects with different configurations in a single hashed collection. For example, this is the existing behavior:
>>> db1 = client.testdb >>> db2 = client.get_database('testdb', read_preference=ReadPreference.SECONDARY) >>> mydict = {} >>> mydict[db1] = 'foo' >>> print(mydict) {Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, replicaset='repl0'), 'test'): 'foo'} >>> mydict[db2] = 'bar' >>> print(mydict) {Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, replicaset='repl0'), 'test'): 'bar'}
We cannot currently use a single hashed collection to store multiple database objects that point to the same logical DB but have different options configured. We will be able to do so if we decide to implement this ticket.
- is related to
-
PYTHON-2466 Make MongoClient, Database and Collection objects hashable
- Closed