<span>mongoimport</span>

 

 

 

 

[work@c3-dba-cloud-cc01 tmp]$ ../mongodb/mongoimport --help
Usage:
  mongoimport <options> <file>

Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.

See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information.

general options:
      --help                                      print usage
      --version                                   print the tool version and exit

verbosity options:
  -v, --verbose=<level>                           more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a
                                                  numeric value, e.g. --verbose=N)
      --quiet                                     hide all log output

connection options:
  -h, --host=<hostname>                           mongodb host to connect to (setname/host1,host2 for replica sets)
      --port=<port>                               server port (can also use --host hostname:port)

kerberos options:
      --gssapiServiceName=<service-name>          service name to use when authenticating using GSSAPI/Kerberos ('mongodb' by default)
      --gssapiHostName=<host-name>                hostname to use when authenticating using GSSAPI/Kerberos (remote server's address by default)

ssl options:
      --ssl                                       connect to a mongod or mongos that has ssl enabled
      --sslCAFile=<filename>                      the .pem file containing the root certificate chain from the certificate authority
      --sslPEMKeyFile=<filename>                  the .pem file containing the certificate and key
      --sslPEMKeyPassword=<password>              the password to decrypt the sslPEMKeyFile, if necessary
      --sslCRLFile=<filename>                     the .pem file containing the certificate revocation list
      --sslAllowInvalidCertificates               bypass the validation for server certificates
      --sslAllowInvalidHostnames                  bypass the validation for server name
      --sslFIPSMode                               use FIPS mode of the installed openssl library

authentication options:
  -u, --username=<username>                       username for authentication
  -p, --password=<password>                       password for authentication
      --authenticationDatabase=<database-name>    database that holds the user's credentials
      --authenticationMechanism=<mechanism>       authentication mechanism to use

namespace options:
  -d, --db=<database-name>                        database to use
  -c, --collection=<collection-name>              collection to use

uri options:
      --uri=mongodb-uri                           mongodb uri connection string

input options:
  -f, --fields=<field>[,<field>]*                 comma separated list of fields, e.g. -f name,age
      --fieldFile=<filename>                      file with field names - 1 per line
      --file=<filename>                           file to import from; if not specified, stdin is used
      --headerline                                use first line in input source as the field list (CSV and TSV only)
      --jsonArray                                 treat input source as a JSON array
      --parseGrace=<grace>                        controls behavior when type coercion fails - one of: autoCast, skipField, skipRow, stop
                                                  (defaults to 'stop') (default: 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 the date
                                                  types, the argument is a datetime layout string. For the binary type, the argument can be one
                                                  of: base32, base64, hex. All other types take an empty argument. Only valid for CSV and TSV
                                                  imports. e.g. zipcode.string(), thumbnail.binary(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

 

 

 

 

 

 

 

导入csv文件:headerline:指明第一行是列名,不需要导入,根据文件第一行判断是否需要加上hedaline

 

一般导入:

mongoimport --username xxx --password yyy --host 10.10.10.10 --port 27017 --authenticationDatabase admin --db=mydb --collection=mytable --type=csv --headerline  --file myfile.cvs

 

先删除再导入:

mongoimport --username xxx --password yyy --host 10.10.10.10 --port 27017 --authenticationDatabase admin --db=mydb --collection=mytable --type=csv --headerline  --drop --file myfile.cvs

 

--upsertFields uid,name,sex 

 

 

 

根据情况确定是否需要先删除原表的数据,如果需要就先删除:

db.table_name.remove( );

 

 

 参数说明:

 

echo "先删除原表数据:--drop" 
echo " 字段为空的不插入:--ignoreBlanks"
echo "部分字段导入:--upsertFields uid,name,sex "

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

###################################################

全部评论

相关推荐

11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
沉淀一会:**圣经 1.同学你面试评价不错,概率很大,请耐心等待;2.你的排名比较靠前,不要担心,耐心等待;3.问题不大,正在审批,不要着急签其他公司,等等我们!4.预计9月中下旬,安心过节;5.下周会有结果,请耐心等待下;6.可能国庆节前后,一有结果我马上通知你;7.预计10月中旬,再坚持一下;8.正在走流程,就这两天了;9.同学,结果我也不知道,你如果查到了也告诉我一声;10.同学你出线不明朗,建议签其他公司保底!11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务