The aggregate command can return either a cursor or store the results in a collection. When returning a cursor or storing the results in a collection, each document in the result set is subject to the BSON Document Size limit, currently 16 megabytes; if any single document that exceeds the BSON Document Size limit, the command will produce an error. The limit only applies to the returned documents; during the pipeline processing, the documents may exceed this size. The db.collection.aggregate() method returns a cursor by default.

each document in the result set is subject to the BSON Document Size limit, currently 16 megabytes

我想知道这个 result set 是否就是 aggregate 返回的 result。如果是,那么 result set 中的单个元素的大小不能超过 16MB,否则整个 result set 的大小总和不能超过 16MB。

结论是 result 中的单个文件不能超过限制。

使用两个 10 MB 的文件进行模拟:

from pymongo import MongoClient
from unittest import TestCase class TestAggregateSizeLimit(TestCase): def setUp(self):
self.client = MongoClient()
self.coll = self.client['test-database']['test-collection'] with open('10mb.txt', 'r') as f:
content = f.read() self.coll.insert_one({
'filename': 1,
'content': content
})
self.coll.insert_one({
'filename': 2,
'content': content
}) def tearDown(self):
self.client.drop_database('test-database') def test_two_aggregate_result(self):
result = list(self.coll.aggregate(
[
{'$sort': {'_id': 1}},
{'$group': {'_id': '$filename', 'content': {'$first': '$content'}}}
]
)) if result:
print('多个文件总和超过 16 MB,但是单个文件没有超过 16MB,没有问题')
else:
print('多个文件总和超过 16 MB,但是单个文件没有超过 16MB,有问题') def test_one_aggregate_result(self):
try:
list(self.coll.aggregate(
[
{'$group': {'_id': None, 'content': {'$push': '$content'}}}
]
))
except Exception as e:
# pymongo==2.8 报错 “$cmd failed: aggregation result exceeds maximum document size (16MB)”
# pymongo==3.7.0 报错 “BSONObj size: 20971635 (0x1400073) is invalid. Size must be between 0 and 16793600(16MB) ”
print(e)
print('结果中的单个文件超过 16MB,有问题')
else:
print('结果中的单个文件超过 16MB,没有问题')

完整代码见 https://github.com/Jay54520/playground/tree/master/mongodb_size_limit

另外,在搜索过程中发现有人说 allowDiskUse 可以解除这个限制,这个是错误的。allowDiskUse 用于避免 pipeline 的 stage 的内存使用超过 100 MB 而报错,而上面的限制是针对单个文件而言。

Pipeline stages have a limit of 100 megabytes of RAM. If a stage exceeds this limit, MongoDB will produce an error. To allow for the handling of large datasets, use the allowDiskUse option to enable aggregation pipeline stages to write data to temporary files.[2]

参考

  1. https://docs.mongodb.com/manual/core/aggregation-pipeline-limits/#result-size-restrictions
  2. https://docs.mongodb.com/manual/core/aggregation-pipeline-limits/#memory-restrictions

最新文章

  1. java线程小结3
  2. 从C#到Objective-C,循序渐进学习苹果开发(5)--利用XCode来进行IOS的程序开发
  3. 批量插入数据 C# SqlBulkCopy使用
  4. codevs 1835 魔法猪学院 A*寻k短路做了一个月卡死在spfa那了/(ㄒoㄒ)/~~
  5. IOS测试程序运行耗时
  6. Xcode6模拟器时BUG导致键盘无法弹出
  7. Css字体中英文对照表
  8. python练习程序_员工信息表_基本实例
  9. 如何用angularjs制作一个完整的表格之二__表格分页功能
  10. 嵌入式环境:挂载开发板根NFS文件系统失败
  11. ComboBox 自动调整组合框下拉部分的宽度
  12. 【IE6的疯狂之十】父级使用padding后子元素绝对定位的BUG
  13. Quartus16.0如何使用TCL脚本
  14. APACHE 安装
  15. Docker 安装 MySQL
  16. Python-Django 模板层
  17. Git diff结果显示分析
  18. jquery将表单序列化
  19. CentOS 7 设置静态IP
  20. Windows Azure Storage (24) 启用Azure Blob日志

热门文章

  1. 常见的安装包制作程序installer
  2. python内置函数之print()
  3. 兼容浏览器的min-height和min-width
  4. 基于jdom 的 xmluti
  5. VBA 获得绝对地址控制焦点的设置
  6. telnet 登陆的方法
  7. RabbitMQ之远程过程调用(RPC)【译】
  8. Cocos2d-x 3.0final 终结者系列教程07-画图节点Node
  9. javascript 屏蔽F5,BackSpace等各种按键
  10. C++ 类的继承二(赋值兼容性原则)