【问题标题】:Does the PyMongo driver aggregate dataPyMongo 驱动程序是否聚合数据
【发布时间】:2011-06-01 16:18:03
【问题描述】:

...在它从 MongoDB 检索所有数据并通过网络传输之后?

我想问的是 - 在传统的数据库场景中,COUNT、SUM 等是在数据库端执行的。 PyMongo 是否通过网络传输所有记录然后进行聚合?

例如,我正在查看来自PyMongo's tutorial 的查询:posts.find({"author": "Mike"}).count()

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    pymongo.cursor.Cursor 的 count() 方法实际上向服务器发送了一个“count”命令,该命令只返回计数,而不是文档。你也可以自己做同样的事情:

    >>> 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}
    

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 2019-08-03
      • 1970-01-01
      • 2020-05-12
      • 2020-01-15
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多