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]#
[root@localhost data]# more t1.json { "_id" : { "$oid" : "4f927e2385b7a6814a0540a0" }, "age" : 2 } [root@localhost data]#
[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]#
[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 } >
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。