【问题标题】:What is right way to implement "count" method on raw MongoDB protocol?在原始 MongoDB 协议上实现“计数”方法的正确方法是什么?
【发布时间】:2016-03-19 11:01:07
【问题描述】:

我已阅读 MongoDB Wire Protocol 并实现了我自己的驱动程序。

我正在尝试实现“计数”方法,该方法返回满足集合条件的文档数。 但是在上面的手册中,我找不到适合计数方法的操作码。

有可能通过执行 OP_QUERY 获取所有需要的文档并将它们计入驱动程序代码。但是看起来效率很低(文档中不必要的内容会在网络上发送)。

我可以通过其他方式有效地实现“计数”方法吗?

【问题讨论】:

    标签: mongodb network-protocols


    【解决方案1】:

    我创建了 mongo-proxy 脚本,它代理 MongoDB 协议并在 MongoDB 服务器和标准 mongo 客户端程序之间运行它。

    因此,我确认请求消息发送如下。

    命令:

    > use test-db
    > db.TestCollection.find({'a': 1}).count()
    3
    

    消息:

    ===== client -> server =====
    type: OP_QUERY
    full_collection_name: test-db.$cmd
    query: {'count': 'TestCollection', 'query': {'a': 1.0}, 'fields': {}}
    
    ===== server -> client =====
    {'n': 3.0, 'ok': 1.0}
    

    我可以从 test-db.$cmd 集合中获取计数值。 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-20
      • 2022-07-22
      • 2011-11-06
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多