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

count_documents and distinct helpers fail in a transaction with directConnection=True

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 4.2
    • Affects Version/s: 4.1, 4.1.1
    • Component/s: None
    • None

      Originally reported here: https://www.mongodb.com/community/forums/t/using-pymonogo-to-execute-transactions-on-single-node-replica-sets/170599

      After the read preference changes in PYTHON-1864, the count_documents and distinct helpers fail when run in a transaction when connected directly to the primary (ie with directConnection=True):

      >>> client = MongoClient(directConnection=True)
      >>> client.topology_description
      <TopologyDescription id: 62bb5c0eca0eb781b7d532a8, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: RSPrimary, rtt: 0.0005203270000002647>]>
      >>> with client.start_session() as s, s.start_transaction():client.t.t.count_documents({}, session=s)
      ... 
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 1843, in count_documents
          return self._retryable_non_cursor_read(_cmd, session)
        File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 1849, in _retryable_non_cursor_read
          return client._retryable_read(func, self._read_preference_for(s), s)
        File "/Users/shane/git/mongo-python-driver/pymongo/mongo_client.py", line 1405, in _retryable_read
          return func(session, server, sock_info, read_pref)
        File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 1838, in _cmd
          result = self._aggregate_one_result(sock_info, read_preference, cmd, collation, session)
        File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 1712, in _aggregate_one_result
          session=session,
        File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 301, in _command
          user_fields=user_fields,
        File "/Users/shane/git/mongo-python-driver/pymongo/pool.py", line 758, in command
          session._apply_to(spec, retryable_write, read_preference, self)
        File "/Users/shane/git/mongo-python-driver/pymongo/client_session.py", line 974, in _apply_to
          "%r" % (read_preference,)
      pymongo.errors.InvalidOperation: read preference in a transaction must be primary, not: PrimaryPreferred(tag_sets=None, max_staleness=-1, hedge=None)
      

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: