Over a million developers have joined DZone.

Announcing PyMongo 2.7 Release Candidate

DZone's Guide to

Announcing PyMongo 2.7 Release Candidate

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.


A few days ago, Bernie Hackett and I shipped a release candidate for PyMongo 2.7, with substantial contributions from Amalia Hawkins and Kyle Erf. This version supports new features in the upcoming MongoDB 2.6, and includes major internal improvements in the driver code. We rarely make RCs before releases, but given the scope of changes it seems wise.

Install the RC like:

pip install \

Please tell us if you find bugs.

MongoDB 2.6 support

For the first time in years, the MongoDB wire protocol is changing. Bernie Hackett updated PyMongo to support the new protocol, while maintaining backwards compatibility with old servers. He also added support for MongoDB's new parallelCollectionScan command, which scans a whole collection with multiple cursors in parallel.

Amalia Hawkins wrote a feature for setting a server-side timeout for long-running operations with the max_time_ms method:

    for doc in collection.find().max_time_ms(1000):
except ExecutionTimeout:
    print "Aborted after one second."

She also added support for the new aggregation operator, $out, which creates a collection directly from an aggregation pipeline. While she was at it, she made PyMongo log a warning whenever your read preference is "secondary" but a command has to run on the primary:

>>> client = MongoReplicaSetClient(
...     'localhost',
...     replicaSet='repl0',
...     read_preference=ReadPreference.SECONDARY)
>>> client.db.command({'reIndex': 'collection'})
UserWarning: reindex does not support SECONDARY read preference
and will be routed to the primary instead.
{'ok': 1}

Bulk write API

Bernie added a bulk write API. It's now possible to specify a series of inserts, updates, upserts, replaces, and removes, then execute them all at once:

bulk = db.collection.initialize_ordered_bulk_op()
bulk.insert({'_id': 1})
bulk.insert({'_id': 2})
bulk.find({'_id': 1}).update({'$set': {'foo': 'bar'}})
bulk.find({'_id': 3}).remove()
result = bulk.execute()

PyMongo collects the operations into a minimal set of messages to the server. Compared to the old style, bulk operations have lower network costs. You can use PyMongo's bulk API with any version of MongoDB, but you only get the network advantage when talking to MongoDB 2.6.

Improved C code

After great effort, I understand why our C extensions didn't like running in mod_wsgi. I wrote an explanation that's more detailed than you want to read. But even better, Bernie fixed our C code so mod_wsgi no longer slows it down or makes it log weird warnings. Finally, I put clear configuration instructions in the PyMongo docs.

Bernie fixed all remaining platform-specific C code. Now you can run PyMongo with its C extensions on ARM, for example if you talk to MongoDB from a Raspberry Pi.

Thundering herd

I overhauled MongoClient so its concurrency control is closer to what I did for MongoReplicaSetClient in the last release. With the new MongoClient, a heavily multithreaded Python application will be much more robust in the face of network hiccups or downed MongoDB servers. You can read details in the bug report.

GridFS cursor

We had several feature requests for querying GridFS with PyMongo, so Kyle Erf implemented a GridFS cursor:

>>> fs = gridfs.GridFS(client.db)
>>> # Find large files:
>>> fs.find({'length': {'$gt': 1024}}).count()
>>> # Find files whose names start with "Kyle":
>>> pattern = bson.Regex('kyle.*', 'i')
>>> cursor = fs.find({'filename': pattern})
>>> for grid_out_file in cursor:
...     print grid_out_file.filename
Kyle Erf

You can browse all 53 new features and fixes in our tracker.


Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Published at DZone with permission of A. Jesse Jiryu Davis, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}