Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-2466

Make MongoClient, Database and Collection objects hashable

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • 3.11.2
    • 3.12, 4.0
    • None
    • None
    • Python 3.8
    • Fully Compatible

    Description

      I'd like to make the Database and Collection objects hashable so that I can use them easily in caching mechanism.

      For instance:

      @functools.lru_cache
      def get_governor(database: pymongo.database.Database, state: str) -> Optional[str]:
       officials = database.officials.find_one({'_id': state})
       if not officials:
       return None
       return officials.get('governor')
      

      Today this is breaking because database is not hashable. It's not hashable because it has an _eq_ method but no _hash_ method.

       

       

      Attachments

        Issue Links

          Activity

            People

              prashant.mital Prashant Mital (Inactive)
              pascal@bayesimpact.org Pascal Corpet
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: