使用mongoexport.exe导出数据 , mongoimport.exe 导出数据
记得命令行不要在末尾加分号 ,也不需要用双引号将输出地址包含起来。
如果加了分号,且与分号,则分号也会被当成文件名的一部分。
如果与分号间有空格存在,则会报错:
ERROR: too many positional options
//导出 .json文件
D:\tool\mongo\bin\mongoexport --username user --password 123456 --collection test --db memo --out D:\tool\mongo\backup\outputfile\test.json
导出json的话,可以不指定-f
默认输出的数据是一行一条记录,是JSON对象,各行之间没有逗号分隔,是以行分隔的。
如果加入--jsonArray 参数,则输出的是一个包含全部数据的JSON对象
//导出 .csv文件
D:\tool\mongo\bin\mongoexport --username user --password 123456 --collection test --db memo -f name,_id,age,addr,di --csv --out D:\tool\mongo\backup\outputfile\test.csv
此格式需要指定-f 即输出的字段,以逗号隔开,否则会报如下错误:
assertion: 9998 you need to specify fields
如果没写-f 虽然也能输出一个CSV文件,但是是空文件。
-f中如果写了不存在的字段,该字段能够输出,但为空值
//导入
D:\tool\mongo\bin\mongoimport.exe -d memo -c jfmall --file D:\tool\mongo\backup\outputfile\test.json -u user -p 123456
如果加入参数-q指定一个查询条件,需要使用单引号括起来,如下所示:
/mongoexport -d page -c Article -q '{"title": "cc"}' -f _id,title,content,images,url --jsonArray > mafengwoArticle.txt
否则,就会出现下面的错误:
ERROR: too many positional options
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
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.