If you’re using server-side features of MongoDB that have a minimum version requirement (like pushing a unique value to a list), it is a good idea to make sure you have the required version running on the server. To check the version of the MongoDB server using PyMongo, you can use something like this:
import pymongo connection = pymongo.Connection() serverVersion = tuple(connection.server_info()['version'].split('.')) requiredVersion = tuple("1.3.3".split(".")) if serverVersion < requiredVersion: # handle the error return 1 ...
It’s important to note that you must connect to the admin database to determine the version number. Otherwise, you will probably run into something like this:
pymongo.errors.OperationFailure: command SON([('buildinfo', 1)]) failed: access denied
If you need to check the version of the server from the interactive prompt, run the following from the mongo prompt:
> db.version() 1.4.2
2 thoughts on “How to get the MongoDB server version using PyMongo”
Good tip! You can also use the server_info() method on a Connection instead of running the command manually. There are some tools (meant just for testing, but might be worth a look) that do this in the `test/version.py` file in the PyMongo source tree.
Updated code to include the slicker-looking “server_info()” call — thanks Mike!