一、导出csv格式:
1,导出csv格式数据,需要同时指定:--type=csv --fields column_a,column_b,column_c
mongoexport --authenticationDatabase admin -hzzz --port=28000 -uxxx -pyyy -d apple -c users --type=csv --fields=_id,username,domain,name,display_name,department_name -o users.csv
[work@xxx mgdb]$ cat apple.csv
_id,name
ObjectId(5fe1c25ad400293f29391e54),igoodful
ObjectId(5fe2e6cb03be46ef38ce5844),guolicheng
2,导出csv格式数据,需要同时指定:--type=csv --fieldFile
[work@hostname tmp]$ cat fields.txt
_id
username
domain
[work@hostname tmp]$
$ mongoexport --authenticationDatabase admin -hzzz --port=28000 -uxxx -pyyy -d apple -c users --type=csv --fieldFile fields.txt -o users.csv
#####################
这里就会按照_id,username,domain三个字段来导出:
_id,username,domain
ObjectId(5db805516e1c8355a15fe80a),glc,google.com
ObjectId(5db805516e1c8355a15fe80b),wjl,apple.com
ObjectId(5db805516e1c8355a15fe80c),zd,airbnb.com
ObjectId(5db805516e1c8355a15fe80d),lt,amazon.com
3,导出csv格式数据文件可以不要列名称 ,加上 --noHeaderLine就能让导出得文件不包含: _id,username,domain ,常用:
[work@hostname tmp]$ cat fields.txt
_id
username
domain
[work@hostname tmp]$
$ mongoexport --authenticationDatabase admin -hzzz --port=28000 -uxxx -pyyy -d apple -c users --type=csv --fieldFile fields.txt --noHeaderLine -o users.csv
#####################
这里就会按照_id,username,domain三个字段来导出:
ObjectId(5db805516e1c8355a15fe80a),glc,google.com
ObjectId(5db805516e1c8355a15fe80b),wjl,apple.com
ObjectId(5db805516e1c8355a15fe80c),zd,airbnb.com
ObjectId(5db805516e1c8355a15fe80d),lt,amazon.com
按照指定条件导出数据:--query '{}'
将名称为“glc”的全部导出:
[work@hostname tmp]$mongoexport --authenticationDatabase admin -hxxx --port=28000 -uyyy -pzzz -d apple -c users --type=csv --fieldFile fields.txt --query '{"username":"glc"}' -o users.csv.glc
2020-10-30T16:57:00.563+0800 connected to: xxx:28000
2020-10-30T16:57:00.743+0800 exported 10 records
[work@hostanme tmp]$ less cas_users.csv.glc
_id,username,domain
ObjectId(7db805916e5c8355a35fe80a),glc,apple.com
ObjectId(7e05a1456e5c836c0c0a1240),glc,apple.com
ObjectId(7e05a3f66e5c836c0c0ac235),glc,apple.cn
ObjectId(7e05a53a6e5c836c0c0b19c4),glc,google.cn
ObjectId(7e05a5ee6e5c836c0c0b5d2d),glc,google.cn
ObjectId(7e05a77d6e5c836c0c0b8fb8),glc,google.cn
ObjectId(7e3920b6d3dbdb333aa04844),glc,google.cn
ObjectId(7e4e625ed3dbdb333aa04f7e),glc,amazon.com
ObjectId(7e96c569e566e174015b7fae),glc,amazon.com
ObjectId(7f4c80abd3dbdb3d702f7a00),glc,apple.com
[work@hostname tmp]$
########################################################
二、mongoexport导出某个集合:
/home/work/mongodb/4.0/bin/mongoexport --authenticationDatabase admin --host 10.10.10.10 --port 28000 --username mongo_backup --password 123456 --db db_name --collection table_name --out /home/work/tmp/table_name
从上面的结果可以看出,我们在导出数据时没有显示指定导出样式 ,默认导出了JSON格式的数据。
[work@xxx mgdb]$ cat apple.json
{"_id":{"$oid":"5fe1c25ad400293f29391e54"},"name":"igoodful"}
{"_id":{"$oid":"5fe2e6cb03be46ef38ce5844"},"name":"guolicheng"}
mongoexport --host hostname --port 28000 --username user --authenticationDatabase admin --collection table_name --db db_name --out table_name.json
mongoexport --uri 'mongodb://user:password@hostname:28000/db_name?authsource=admin' --collection table_name --out table_name.json
mongoexport --db db_name --collection table_name --query '{"dept": "ABC", date: { $gte: { "$date": "2018-01-01T00:00:00.000Z" } }}'
uri规则:
--uri "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"
[work@hostname tmp]$ mongoimport --help
Usage:
mongoimport <options> <file>: stop)
--type=<type> input format to import: json, csv, or tsv (defaults to 'json') (default: json)
--columnsHaveTypes indicated that the field list (from --fields, --fieldsFile, or --headerline) specifies types;
They must be in the form of '<colName>.<type>(<arg>)'. The type can be one of: auto, binary,
bool, date, date_go, date_ms, date_oracle, double, int32, int64, string. For each of(base64)
ingest options:
--drop drop collection before inserting documents
--ignoreBlanks ignore fields with empty values in CSV and TSV
--maintainInsertionOrder insert documents in the order of their appearance in the input source
-j, --numInsertionWorkers=<number> number of insert operations to run concurrently (defaults to 1) (default: 1)
--stopOnError stop importing at first insert/upsert error
--mode=[insert|upsert|merge] insert: insert only. upsert: insert or replace existing documents. merge: insert or modify
existing documents. defaults to insert
--upsertFields=<field>[,<field>]* comma-separated fields for the query part when --mode is set to upsert or merge
--writeConcern=<write-concern-specifier> write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync:
true, j: true}'
--bypassDocumentValidation bypass document validation
#################
参数说明:
--jsonArray:默认情况下,该mongoexport
命令为每个Mongo文档使用一个JSON文档写入数据。使用可选--jsonArray
参数将mongoexport
命令的输出修改为单个JSON数组
--pretty:还是一个可选参数,以漂亮的JSON格式输出集合
###############################