...after it retrieves all of it from MongoDB and transferring it over the network?
What I'm trying to ask is - in a traditional DB scenario, the COUNT, SUM etc are performed at the DB end. Does PyMongo transfer all the records over the network and then do the aggregation?
For example, I'm looking at the query from PyMongo's tutorial: posts.find({"author": 开发者_高级运维"Mike"}).count()
The count() method of pymongo.cursor.Cursor actually sends a 'count' command to the server that only returns the count, not the documents. You can do the same thing yourself:
>>> db = c.foo
>>> for doc in db.things.find(): print doc
...
{u'_id': ObjectId('4de671821121812a0087101b'), u'foo': u'bar'}
{u'_id': ObjectId('4de671ea1121812a0087101c'), u'buzz': u'baz'}
>>> db.command('count', 'things', query={'foo': 'bar'})
{u'ok': 1.0, u'n': 1.0}
精彩评论