MongoDB数据导入和导出
元数据需要事先创建好。
mongoimport
这个命令可以导入单个JSON/CSV/TSV格式的文件。文件的每一行都要是指定格式的标准格式。
需要指定一个数据库(database)和一个collection(相当于关系数据库中的表)。
×××××××××××××
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 ("left,right" for pairs)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod data files in the given path,
instead of connecting to a mongod instance - 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
-f [ --fields ] arg comma seperated 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
×××××××××××××
MongoDB提供了比JSON丰富的多的数据格式,例如JSON中没有date数据类型,但是通过如下结构:
{"somefield" : 123456, "created_at" : {"$date" : 1285679232000}}
mongoimport 将create_at转换为Date类型数据了。
注意:像这里的$date这样的实现确定的数据类型必须加双引号,否则无法正确解析。
mongoexport
这个工具可以将一个collection导出为JSON为CSV格式的文件。可以指定导出哪些数据项,也可以根据给
定的条件导出数据。
如果想导出CSV格式的文件,需要指定数据项的次序。
×××××××××××××
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 ("left,right" for pairs)
-d [ --db ] arg database to use
-c [ --collection ] arg where 'arg' is the collection to use
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod data files in the given path,
instead of connecting to a mongod instance - 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
-q [ --query ] arg query filter, as a JSON string
-f [ --fields ] arg comma seperated list of field names e.g. -f name,age
--csv export to csv instead of json
-o [ --out ] arg output file; if not specified, stdout is used
×××××××××××××
mongodump
这个用于对数据库进行备份。
×××××××××××××
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 ("left,right" for pairs)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod data files in the given path,
instead of connecting to a mongod instance - 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
-o [ --out ] arg (=dump) output directory
-q [ --query ] arg json query
×××××××××××××
例如:导出所有数据库中的所有collects,使用mongodump加上--host参数就可以了,在本地会产生一个dump命名的文件夹。
×××××××××××××
$ ./mongodump --host prod.example.com
connected to: prod.example.com
all dbs
DATABASE: log to dump/log
log.errors to dump/log/errors.bson
713 objects
log.analytics to dump/log/analytics.bson
234810 objects
DATABASE: blog to dump/blog
blog.posts to dump/log/blog.posts.bson
59 objects
DATABASE: admin to dump/admin
×××××××××××××
例如:导出指定数据库中的一个指定的collection,导出结果是一个.bson文件。
$ ./mongodump --db blog --collection posts
connected to: 127.0.0.1
DATABASE: blog to dump/blog
blog.posts to dump/blog/posts.bson
59 objects
在mongodb 1.7.0版本或更高的版本中,还可以将指定的collect通过标准输出流输出。
这时需要指定 --out 参数
$ ./mongodump --db blog --collection posts --out - > blogposts.bson
一次只能导出一个collection到标准输出流文件中。
mongorestore
这个工具获取mongodump的输出结果然后重新存储。重新存储的过程会加上索引。
×××××××××××××
usage: ./mongorestore [options] [directory or filename to restore from]
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 ("left,right" for pairs)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod data files in the given path,
instead of connecting to a mongod instance - 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
--drop drop each collection before import
--objcheck validate object before inserting
--filter arg filter to apply before inserting
--drop drop each collection before import
--indexesLast wait to add indexes (faster if data isn't inserted in
index order)
×××××××××××××
注意:如果不想创建索引的话,可以从数据库导出的文件夹(dump文件夹)中删除system.indexses.bson文件。
bsondump
这个是在1.6版本中新增加的。
这个工具将一个bson文件转换为一个json/debug输出文件。
×××××××××××××××
usage: ./bsondump [options] [filename]
options:
--help produce help message
--type arg (=json) type of output: json,debug
××××××××××××××××
分享到:
相关推荐
找了好久才找到的,还不错!有喜欢的可以下载哦
几天想着公司要用MongoDB,自然就要用到数据导入导出,就自己学习了一下。本文介绍了mongoDb导入导出数据的方法,有需要的朋友参考下。
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。导入导出很管用啊
数据库备份和数据恢复的重要性,我想大家都知道,这里就举例说明如何操作数据备份,数据恢复的实例: 创建测试数据 创建db:testdb,collection:user,插入10条记录 mongo MongoDB shell version: 3.0.2 connecting to...
day1:MongoDB数据库与其他数据库区别介绍及学习方法 day2:MongoDB运行环境搭建及运行 ...day11:MongoDB数据库导入导出与监控 day12:MongDB数据库常见故障及解决方案 day13:在以后工作中应用的注意点
支持导出,导入,压缩打包,解压
本身MONGODB 是支持两种数据的迁出和导入的方式,当然后面也会有两个复制集合之间的数据迁移的东西。 一般小型系统使用mongodump 和 mongorestore 来进行系统的备份和恢复mongodump可以用来转储整个数据库、集合或...
需求是这样的:需要修改数据库中某个表的所有数据,所以,要全部导出,然后修改,修改完之后,再把修改后的数据给再导入到mongo去。 具体如下: 备份,导出一张表为json文件 具体命令: mongoexport --host ...
Java使用POI将Mongo数据库数据导出到Excel文件;包含java 测试文件和各种jar包 ,bson-3.4.2.jar ,mongodb-driver-3.4.2.jar,mongodb-driver-core-3.4.2.jar,poi-3.9-20121203.jar
Mongo 数据导出、导入工具
01 MongoDB的安装与基本操作.docx; 02 MongoDB数据导入导出备份恢复篇.docx 03 .......
主要介绍了mongodb安装、常用命令、举例、数据导入导出及备份恢复,适用于初学者学习使用。
2.4 MongoDB数据类型 2.4.1 基本数据类型 2.4.2 数字类型 2.4.3 日期类型 2.4.4 数组类型 2.4.5 内嵌文档类型 2.4.6 _id键和ObjectId对象 2.5 MongoDB的下载和安装 2.5.1 MongoDB的下载 ...
该视频教程内容丰富、循序渐进,由浅入深,包括:mongodb的安装及启动,基础命令,数据存储结构,高级查询,索引,主从副本集,分片 ,数据导入与导出,性能监控与运维管理等,附带源码和PTT,百度云盘下载。
【学习目标】 1:安装及启动 2:mongodb的数据存储结构 3:高级查询技巧 4:mongodb索引优化 5:mongodb主从副本集(replica set) ...7:数据导入与导出 8:性能监控与运维管理 9:实战:3台服务器实战亿级短网址项目
数据导入导出: json或cvs格式,每次一个collection 数据导出:mongoexport.exe --help mongoexport.exe -d test -c t001 -o D:\Cloud\NoSql\mongodb2\data\backup\test_t001.json //导出test库的t001(集合)表数据 ...
mongodb数据备份和还原主要分为二种,一种是针对于库的mongodump和mongorestore,一种是针对库中表的mongoexport和mongoimport。