千家信息网

mongodb导入和导出数据的方法

发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,这篇文章主要介绍"mongodb导入和导出数据的方法",在日常操作中,相信很多人在mongodb导入和导出数据的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"mon
千家信息网最后更新 2025年11月13日mongodb导入和导出数据的方法

这篇文章主要介绍"mongodb导入和导出数据的方法",在日常操作中,相信很多人在mongodb导入和导出数据的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"mongodb导入和导出数据的方法"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

MongoDB提供了mongoexport工具,可以把一个collection导出成json格式或csv格式的文件。可以指定导出哪些数据项,也可以根据给定的条件导出数据。工具帮助信息如下:

[root@localhost bin]# ./mongoexport --help  options:  --help produce help message   -v [ --verbose ] be more verbose (include multiple times for more   verbosity e.g. -vvvvv)   -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)   --port arg server port. Can also use --host hostname:port   --ipv6 enable IPv6 support (disabled by default)   -u [ --username ] arg username   -p [ --password ] arg password   --dbpath arg directly access mongod database files in the given   path, instead of connecting to a mongod server -   needs to lock the data directory, so cannot be used   if a mongod is currently accessing the same path   --directoryperdb if dbpath specified, each db is in a separate   directory   -d [ --db ] arg database to use   -c [ --collection ] arg collection to use (some commands)   -f [ --fields ] arg comma separated list of field names e.g. -f name,age   --fieldFile arg file with fields names - 1 per line   -q [ --query ] arg query filter, as a JSON string   --csv export to csv instead of json   -o [ --out ] arg output file; if not specified, stdout is used   --jsonArray output to a json array rather than one object per   line   [root@localhost bin]#

下面我们将以一个实际的例子说明,此工具的用法:

将foo库中的表t1导出成json格式:

[root@localhost bin]# ./mongoexport -d foo -c t1 -o /data/t1.json   connected to: 127.0.0.1   exported 1 records   [root@localhost bin]#

导出成功后我们看一下/data/t1.json文件的样式,是否是我们所希望的:

root@localhost data]# more t1.json   { "_id" : { "$oid" : "4f927e2385b7a6814a0540a0" }, "age" : 2 }   [root@localhost data]#

通过以上说明导出成功,但有一个问题,要是异构数据库的迁移怎么办呢?例如我们要将MongoDB的数据导入到MySQL该怎么办呢?MongoDB提供 了一种csv的导出格式,就可以解决异构数据库迁移的问题了. 下面将foo库的t2表的age和name列导出, 具体如下:

[root@localhost bin]# ./mongoexport -d foo -c t2 --csv -f age,name -o /data/t2.csv   connected to: 127.0.0.1   exported 1 records   [root@localhost bin]#

查看/data/t2.csv的导出结果

[root@localhost data]# more t2.csv   age,name   1,"wwl"   [root@localhost data]#

mongoimport导入工具

MongoDB提供了mongoimport工具,可以把一个特定格式文件中的内容导入到某张collection中。工具帮助信息如下:

[root@localhost bin]# ./mongoimport --help   options:   --help produce help message   -v [ --verbose ] be more verbose (include multiple times for more   verbosity e.g. -vvvvv)   -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)   --port arg server port. Can also use --host hostname:port   --ipv6 enable IPv6 support (disabled by default)   -u [ --username ] arg username   -p [ --password ] arg password   --dbpath arg directly access mongod database files in the given   path, instead of connecting to a mongod server -   needs to lock the data directory, so cannot be used   if a mongod is currently accessing the same path   --directoryperdb if dbpath specified, each db is in a separate   directory   -d [ --db ] arg database to use   -c [ --collection ] arg collection to use (some commands)   -f [ --fields ] arg comma separated list of field names e.g. -f name,age   --fieldFile arg file with fields names - 1 per line   --ignoreBlanks if given, empty fields in csv and tsv will be ignored   --type arg type of file to import. default: json (json,csv,tsv)   --file arg file to import from; if not specified stdin is used   --drop drop collection first   --headerline CSV,TSV only - use first line as headers   --upsert insert or update objects that already exist   --upsertFields arg comma-separated fields for the query part of the   upsert. You should make sure this is indexed   --stopOnError stop importing at first error rather than continuing   --jsonArray load a json array, not one item per line. Currently   limited to 4MB.

下面我们将以一人实际的例子说明,此工具的用法:
先看一下foo库中的t1表数据:

> db.t1.find();   { "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   >

t1其中有一条age=5的记录, 我们再看一下json文件中的数据是什么样子的:

[root@localhost data]# more t1.json   { "_id" : { "$oid" : "4f937a56450beadc560feaa7" }, "age" : 8 }   [root@localhost data]#

可以看到t1.json文件中有一条age=8的数据,下面我们将用mongoimport工具将json文件中的记录导入到t1表中:

[root@localhost bin]# ./mongoimport -d foo -c t1 /data/t1.json   connected to: 127.0.0.1   imported 1 objects

工具返回信息说明向表中插入了一条记录. 我们进库里实际验证一下:

[root@localhost bin]# ./mongo   MongoDB shell version: 1.8.1   connecting to: test   > use foo   switched to db foo   > db.t1.find();   { "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   { "_id" : ObjectId("4f937a56450beadc560feaa7"), "age" : 8 }   >

到此,关于"mongodb导入和导出数据的方法"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

数据 工具 文件 方法 格式 e.g. 学习 帮助 信息 实际 问题 成功 例子 怎么办 数据库 更多 实用 接下来 内容 数据项 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网吧服务器连接显示器用什么线 网络技术毕业设计方向怎么写 微信后端服务器超时 樱花服务器搭建ssr免流 网络技术同义词大全 谁等有关机关依照网络安全法 全球主权财富基金数据库 网络安全 述职报告 上位机软件连接数据库 服务器安装什么路由器 如何做好国家网络安全贡献 分部数据库服务器的IP地址无效 安徽戴尔服务器订制 imts网络安全 博客项目需求分析数据库 如何玩到主播同款宝可梦服务器 万方数据库可以查到什么 信息网络安全审核证 上海服务器迁移公司电话 数据库原理与sql题库 厦门软件开发公司排名招聘 内江市网络安全保卫支队 网络安全保护措施和要求 昌平区推广软件开发热线 先优化数据库还是先清理磁盘 网络安全专业化 网络安全法教育培训记录 泰拉瑞亚手游有服务器吗 网络技术提供了相关技术应用 网络安全知识防护技能视频
0