|
| 1 | +Frequently Encountered Issues |
| 2 | +============================= |
| 3 | + |
| 4 | +Also see the :ref:`TLSErrors` section. |
| 5 | + |
| 6 | +.. contents:: |
| 7 | + |
| 8 | +Server reports wire version X, PyMongo requires Y |
| 9 | +------------------------------------------------- |
| 10 | + |
| 11 | +When one attempts to connect to a <=3.4 version server, PyMongo will throw the following error:: |
| 12 | + |
| 13 | + >>> client.admin.command('ping') |
| 14 | + ... |
| 15 | + pymongo.errors.ConfigurationError: Server at localhost:27017 reports wire version 5, but this version of PyMongo requires at least 6 (MongoDB 3.6). |
| 16 | + |
| 17 | +This is caused by the driver being too new for the server it is being run against. |
| 18 | +To resolve this issue either upgrade your database to version >= 3.6 or downgrade to PyMongo 3.x which supports MongoDB >= 2.6. |
| 19 | + |
| 20 | + |
| 21 | +'Cursor' object has no attribute '_Cursor__killed' |
| 22 | +-------------------------------------------------- |
| 23 | + |
| 24 | +On versions of PyMongo <3.9, when supplying invalid arguments the constructor of Cursor, |
| 25 | +there will be a TypeError raised, and an AttributeError printed to ``stderr``. The AttributeError is not relevant, |
| 26 | +instead look at the TypeError for debugging information:: |
| 27 | + |
| 28 | + >>> coll.find(wrong=1) |
| 29 | + Exception ignored in: <function Cursor.__del__ at 0x1048129d8> |
| 30 | + ... |
| 31 | + AttributeError: 'Cursor' object has no attribute '_Cursor__killed' |
| 32 | + ... |
| 33 | + TypeError: __init__() got an unexpected keyword argument 'wrong' |
| 34 | + |
| 35 | +To fix this, make sure that you are supplying the correct keyword arguments. |
| 36 | +In addition, you can also upgrade to PyMongo >=3.9, which will remove the spurious error. |
| 37 | + |
| 38 | + |
| 39 | +MongoClient fails ConfigurationError |
| 40 | +------------------------------------ |
| 41 | + |
| 42 | +This is a common issue stemming from using incorrect keyword argument names. |
| 43 | + |
| 44 | + >>> client = MongoClient(wrong=1) |
| 45 | + ... |
| 46 | + pymongo.errors.ConfigurationError: Unknown option wrong |
| 47 | + |
| 48 | +To fix this, check your spelling and make sure that the keyword argument you are specifying exists. |
| 49 | + |
| 50 | + |
| 51 | +DeprecationWarning: count is deprecated |
| 52 | +--------------------------------------- |
| 53 | + |
| 54 | +PyMongo no longer supports :meth:`pymongo.cursor.count`. |
| 55 | +Instead, use :meth:`pymongo.collection.count_documents`:: |
| 56 | + |
| 57 | + >>> client = MongoClient() |
| 58 | + >>> d = datetime.datetime(2009, 11, 12, 12) |
| 59 | + >>> list(client.db.coll.find({"date": {"$lt": d}}, limit=2)) |
| 60 | + [{'_id': ObjectId('6247b058cebb8b179b7039f8'), 'date': datetime.datetime(1, 1, 1, 0, 0)}, {'_id': ObjectId('6247b059cebb8b179b7039f9'), 'date': datetime.datetime(1, 1, 1, 0, 0)}] |
| 61 | + >>> client.db.coll.count_documents({"date": {"$lt": d}}, limit=2) |
| 62 | + 2 |
| 63 | + |
| 64 | +Note that this is NOT the same as ``Cursor.count_documents`` (which does not exist), |
| 65 | +this is a method of the Collection class, so you must call it on a collection object |
| 66 | +or you will receive the following error:: |
| 67 | + |
| 68 | + >>> Cursor(MongoClient().db.coll).count() |
| 69 | + Traceback (most recent call last): |
| 70 | + File "<stdin>", line 1, in <module> |
| 71 | + AttributeError: 'Cursor' object has no attribute 'count' |
| 72 | + >>> |
| 73 | + |
| 74 | +Timeout when accessing MongoDB from PyMongo with tunneling |
| 75 | +---------------------------------------------------------- |
| 76 | + |
| 77 | +When attempting to connect to a replica set MongoDB instance over an SSH tunnel you |
| 78 | +will receive the following error:: |
| 79 | + |
| 80 | + File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1560, in count |
| 81 | + return self._count(cmd, collation, session) |
| 82 | + File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1504, in _count |
| 83 | + with self._socket_for_reads() as (sock_info, slave_ok): |
| 84 | + File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__ |
| 85 | + return self.gen.next() |
| 86 | + File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 982, in _socket_for_reads |
| 87 | + server = topology.select_server(read_preference) |
| 88 | + File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 224, in select_server |
| 89 | + address)) |
| 90 | + File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 183, in select_servers |
| 91 | + selector, server_timeout, address) |
| 92 | + File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 199, in _select_servers_loop |
| 93 | + self._error_message(selector)) |
| 94 | + pymongo.errors.ServerSelectionTimeoutError: localhost:27017: timed out |
| 95 | + |
| 96 | +This is due to the fact that PyMongo discovers replica set members using the response from the isMaster command which |
| 97 | +then contains the address and ports of the other members. However, these addresses and ports will not be accessible through the SSH tunnel. Thus, this behavior is unsupported. |
| 98 | +You can, however, connect directly to a single MongoDB node using the directConnection=True option with SSH tunneling. |
0 commit comments