当前位置: 首页 > news >正文

使用 mongoimport 导入本地 JSON 文件到 MongoDB 及数据查看指南

      在项目中,我们经常需要将本地 JSON 文件批量导入 MongoDB 数据库。本文以 Ubuntu 22.04 环境为例,详细记录了如何安装 mongoimport 工具、正确导入多个 JSON 文件,以及查看导入后的数据。


一、环境介绍

  • 操作系统:Ubuntu 22.04.5 LTS

  • MongoDB 版本:已安装并运行中

  • 目标数据库:database

  • 目标集合:instructions

  • 本地 JSON 文件目录:/home/yhzhao/sun/week1/test_result/


二、安装 mongoimport 工具(bash下)

Ubuntu 22.04 默认源没有 mongo-tools,需从 MongoDB 官方下载工具包:

wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2204-x86_64-100.9.4.tgz
tar -zxvf mongodb-database-tools-ubuntu2204-x86_64-100.9.4.tgz
cd mongodb-database-tools-ubuntu2204-x86_64-100.9.4
export PATH=$PATH:$(pwd)/bin

确认安装:

mongoimport --version

三、批量导入 JSON 文件

假设目录中每个 JSON 文件格式是多行独立 JSON 对象,例如:

{ "title": "任务1", "content": "步骤A" }
{ "title": "任务2", "content": "步骤B" }

批量导入命令:

for file in /home/yhzhao/sun/week1/test_result/*.json; domongoimport --db database --collection instructions --file "$file"
done

注意:不要加 --jsonArray,除非文件是标准 JSON 数组格式。


四、常见问题及解决

错误提示:

Failed: error reading separator after document #1: bad JSON array format - found no opening bracket '[' in input source

产生原因:

用了 --jsonArray,但 JSON 文件不是数组格式。

解决办法:

去掉 --jsonArray 参数,使用默认方式导入。


五、查看导入数据

进入 MongoDB shell:

mongosh --host localhost:27017 
mongosh
  1. 选择数据库

    use database
    
  2. 查看数据条数:

    db.instructions.countDocuments()
    
  3. 查看部分数据:

    db.instructions.find().limit(5).pretty()
    
  4. 查看集合列表:

    show collections
    
  5. 退出 shell:

    exit
    

    六、总结

    • Ubuntu 22.04 需手动安装 MongoDB Database Tools 来使用 mongoimport

    • JSON 文件格式决定是否需要 --jsonArray 参数;

    • 结合 Bash 循环批量导入多个文件;

    • 导入成功后,Mongo shell 提供简洁高效的数据查看方式。


    文章转载自:
    http://anaphylactoid.lbooon.cn
    http://anthill.lbooon.cn
    http://armlet.lbooon.cn
    http://bookish.lbooon.cn
    http://avoid.lbooon.cn
    http://adnominal.lbooon.cn
    http://alcor.lbooon.cn
    http://centrosome.lbooon.cn
    http://bullshit.lbooon.cn
    http://alcula.lbooon.cn
    http://bellmouthed.lbooon.cn
    http://anthema.lbooon.cn
    http://agrostologist.lbooon.cn
    http://amplitude.lbooon.cn
    http://apodictic.lbooon.cn
    http://ballyhoo.lbooon.cn
    http://atomics.lbooon.cn
    http://campbellism.lbooon.cn
    http://api.lbooon.cn
    http://appose.lbooon.cn
    http://chatoyancy.lbooon.cn
    http://bringdown.lbooon.cn
    http://balladize.lbooon.cn
    http://buttress.lbooon.cn
    http://bennett.lbooon.cn
    http://butanol.lbooon.cn
    http://banally.lbooon.cn
    http://azof.lbooon.cn
    http://activize.lbooon.cn
    http://alphabetically.lbooon.cn
    http://www.dtcms.com/a/281075.html

    相关文章:

  1. 微信小程序入门实例_____从零开始 开发一个每天记账的微信小程序
  2. Rust语言
  3. Isaac Sim仿真赋能机器人工作流,推动具身智能在机器人领域研究
  4. 深入解析:磁盘级文件与内存级(被打开)文件的本质区别与联系
  5. MySQL锁机制与SQL优化详解
  6. Vue 中 effectScope() 的全面解析与实战应用
  7. 虚拟机删除操作
  8. lanch4j将jar转成exe
  9. 文心4.5开源背后的战略棋局:百度为何选择All in开放?
  10. Django基础(二)———URL与映射
  11. 10 款游戏设计工具深度解析,打造卓越游戏项目
  12. 在Autodl服务器中使用VNC建立图形界面
  13. MySQL查询今天、昨天、上周、近30天、去年等的数据的方法
  14. [锂电池]锂电池入门指南
  15. Android 多语言适配(I18n)
  16. 逻辑回归案例
  17. Prompt提示工程
  18. just thinking for vocabulary
  19. Visual Prompt Tuning核心思路讲解(个人总结)
  20. 《大数据技术原理与应用》实验报告七 熟悉 Spark 初级编程实践
  21. 物联网系统中MQTT设备数据的保存方法
  22. 了解 Android 内存使用情况 - Google I/O 大会演讲
  23. ethers.js-8-bigNmber和callstatic模拟
  24. 【Android】日志的使用
  25. 《黑马笔记》 --- C++核心编程
  26. 10分钟搞定!Chatbox+本地知识库=你的私人语音导师:企业级全栈实现指南
  27. etcd压缩历史版本
  28. 安装MATLAB流程中遇到的问题
  29. wpf Canvas 动态增加右键菜单
  30. css:flex:1;是谁的缩写