MongoDB 聚合结果大小限制
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]
参考
- https://docs.mongodb.com/manual/core/aggregation-pipeline-limits/#result-size-restrictions
- https://docs.mongodb.com/manual/core/aggregation-pipeline-limits/#memory-restrictions
MongoDB 聚合结果大小限制的更多相关文章
- MongoDB 聚合管道(Aggregation Pipeline)
管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...
- Mongodb学习笔记四(Mongodb聚合函数)
第四章 Mongodb聚合函数 插入 测试数据 ;j<;j++){ for(var i=1;i<3;i++){ var person={ Name:"jack"+i, ...
- mongodb MongoDB 聚合 group
MongoDB 聚合 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 基本语法为:db.col ...
- MongoDB 聚合
聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...
- MongoDB聚合
--------------------MongoDB聚合-------------------- 1.aggregate(): 1.概念: 1.简介 ...
- MongoDB 聚合分组取第一条记录的案例及实现
关键字:MongoDB: aggregate:forEach 今天开发同学向我们提了一个紧急的需求,从集合mt_resources_access_log中,根据字段refererDomain分组,取分 ...
- mongodb MongoDB 聚合 group(转)
MongoDB 聚合 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 基本语法为:db.col ...
- mongodb聚合 group
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 基本语法为:db.collection.agg ...
- MongoDB 聚合(管道与表达式)
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). aggregate() 方法 MongoDB中 ...
随机推荐
- 使用c++的cocos2d-x-3.0rc1程序公布apk
(如今cocos2dx-x-3.0正式版已经出了.之前用的cocos2d-x-3.0rc1,就先用这个版本号吧) 0. 完毕C++项目 在cmd下使用cocos.py new命令,然后习惯性的在win ...
- 【转】如何把hadoop-1.x源码关联到Eclipse工程
[转]http://www.tuicool.com/articles/mIb2EzU
- vss安装及服务器端、客户端配置图文教程
安装VSS 一.双击setup. 之后点完成.就安装完了! 服务器端VSS配置 一.选择开始——所有程序——打开 一直下一步 到完成 二.然后再打开 说明:把这个enable rights and ...
- 2016 年 Java 优秀文章
https://www.ibm.com/developerworks/cn/java/j-2016-java-good-article/index.html
- C语言 · Sine之舞
基础练习 Sine之舞 时间限制:1.0s 内存限制:512.0MB 问题描述 最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数基本功.所以他准备和奶 ...
- rt serial 的编写
/* * Copyright (C) 2005-2007 Jan Kiszka <jan.kiszka@web.de>. * * Xenomai is free software; you ...
- C#中利用JQuery实现视频网站的缩略图采集
最近有朋友想要采集优酷的视频标题和缩略图 (哈哈, 并非商业目的). 找到我帮忙, 考虑到有我刚刚发布的SpiderStudio, 我毫不犹豫的答应了. 首先在网页上视频的基本结构为: div.v - ...
- sudo非交互式输入密码
sudo非交互式输入密码 编辑 删除 我们在使用sudo命令的时候,为了避免交互,可以使用 echo 'password' |sudo -S cmd 这样的方式,通过管道传入密码,就不用手动输入了. ...
- web automation 常用技术比较
selenium2支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaDriver,ChromeDriver)驱动真实浏览器完成测试. 除此之外, ...
- Differential Geometry之第六章平面曲线的整体性质
第六章.平面曲线的整体性质 1.平面的闭曲线 1.1.切线的旋转指数定理 1.2.等周不等式与圆的几何特性 ,其中 2.平面的凸曲线 支撑函数: 2.1.Minkowski问题 2.2.四顶点定理