Python MongoDB-pymongo.errors.DocumentTooLarge错误
我有一个REST接口,它将数据发布到MongoDB。数据基本上是作为嵌套JSON结构的文件系统结构,其中JSON中的每个级别都有一个键“children”。如果是文件夹或文件,则子目录包含子目录,然后将文件内容作为字符串数组包含。该数据稍后将由另一个基于Java的应用程序使用。JSON数据的外观示例如下:Python MongoDB-pymongo.errors.DocumentTooLarge错误,python,mongodb,gridfs,Python,Mongodb,Gridfs,我有一个REST接口,它将数据发布到MongoDB。数据基本上是作为嵌套JSON结构的文件系统结构,其中JSON中的每个级别都有一个键“children”。如果是文件夹或文件,则子目录包含子目录,然后将文件内容作为字符串数组包含。该数据稍后将由另一个基于Java的应用程序使用。JSON数据的外观示例如下: { 'name': 'sys', 'type': 'system', 'path': 'sys', 'children': [{ 'name':
{
'name': 'sys',
'type': 'system',
'path': 'sys',
'children': [{
'name': 'folder1',
'type': 'folder',
'path': 'sys/folder1',
'children': [{
'name': 'folder2',
'type': 'folder',
'path': 'sys/folder1/folder2',
'children': [{
'name': 'textf1.txt',
'type': 'file',
'path': 'sys/folder1/folder2/textf1.txt',
'children': ['abc', 'def']
}, {
'name': 'textf2.txt',
'type': 'file',
'path': 'sys/folder1/folder2/textf2.txt',
'children': ['a', 'b', 'c']
}]
}, {
'name': 'text1.txt',
'type': 'file',
'path': 'sys/folder1/text1.txt',
'children': ['aaa', 'bbb', 'ccc']
}]
}],
'_id': ObjectId('5d1211ead866fc19ccdf0c77')
}
我分析文件系统结构的方式是:
def path_to_dict(path, child=False):
d = {'name': os.path.basename(path)}
if os.path.isdir(path):
if not child:
d['type'] = "system"
else:
d['type'] = "folder"
d['path'] = os.path.relpath(path)
d['children'] = [path_to_dict(os.path.join(path, x),
child=True) for x in os.listdir\(path)]
else:
if not child:
d['type'] = "system"
else:
d['type'] = "file"
d['path'] = os.path.relpath(path)
with open(path, 'r', encoding="utf-8", errors='ignore') as myfile:
content = myfile.read().splitlines()
d['children'] = content
return d
现在,我解析的所有文件系统都成功运行,只有一个系统出现错误:
pymongo.errors.DocumentTooLarge:BSON文档太大(74101706字节)-连接的服务器支持最大为16793598字节的BSON文档。
我知道文档大小有16MB的限制,很多人建议使用GridFS来分解文件。我的问题是,没有一个文件会导致如此大的文档大小,而是有多个小文档
我的服务器POST端点如下所示:
def post(self, collection_name):
user = mongo.db[collection_name]
client_request = request.get_json(force=True)
user.insert(client_request)
这个问题的解决方案是什么?GridFS在这样的场景中有用吗
编辑:失败的文件系统结构有多个子文件夹,其中包含C文件,其内容被视为字符串,并作为字符串数组存储在嵌套的JSON中