You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用mongoimport导入JSON压缩包至MongoDB?解决导入报错

解决mongoimport直接导入ZIP压缩JSON文件的问题

mongoimport本身不支持直接读取ZIP压缩包,你遇到的报错就是因为它把ZIP文件当成纯文本解析,自然识别不了JSON数组格式。不过可以通过系统自带的解压工具配合管道,实现边解压边导入,不用先把文件解压到本地。

Windows系统(PowerShell)

如果ZIP包内仅包含目标JSON文件,用Windows自带的tar命令即可:

tar -xf ~\downloads\inventory.crud.json.zip -O | mongoimport --db test --collection inventory --authenticationDatabase admin --username <user> --password <password> --drop --jsonArray

-O参数会将解压后的文件内容直接输出到标准输出,再通过管道|传递给mongoimport处理。

也可以用PowerShell的Expand-Archive命令(会临时生成解压文件,用完建议手动删除临时目录):

Get-Content (Expand-Archive -Path ~\downloads\inventory.crud.json.zip -DestinationPath .\temp -PassThru).FullName | mongoimport --db test --collection inventory --authenticationDatabase admin --username <user> --password <password> --drop --jsonArray

Linux/macOS系统

用unzip命令配合管道实现:

unzip -p ~/downloads/inventory.crud.json.zip | mongoimport --db test --collection inventory --authenticationDatabase admin --username <user> --password <password> --drop --jsonArray

-p参数指定将解压后的内容输出到标准输出,不写入本地文件。

额外提示

如果你的压缩文件是.gz格式(gzip压缩),mongoimport原生支持--gzip参数,直接添加参数即可读取,无需额外工具:

mongoimport --db test --collection inventory --authenticationDatabase admin --username <user> --password <password> --drop --file ~/downloads/inventory.crud.json.gz --jsonArray --gzip

注意:确保ZIP包内只有一个目标JSON文件,多个文件混在一起会导致JSON格式错误。

内容的提问来源于stack exchange,提问作者Umer Farooq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 12:24:13