zoukankan      html  css  js  c++  java
  • mongoexport/mongimport命令详解

    mongoexpot 命令参考文档:

    https://docs.mongodb.com/v4.2/reference/program/mongoexport/

    创建测试数据

    use czg
    for(i=1;i<=100;i++){db.t1.insert({id:i})}
    for(i=1;i<=100;i++){db.t2.insert({id:i,name:"ceshi2"})}
    for(i=1;i<=100;i++){db.t3.insert({id:i,name:"ceshi3"})}
    for(i=1;i<=100;i++){db.a1.insert({id:i,name:"ceshi4"})}
    db.t1.createIndex({id:1})
    db.t1.createIndex({name:1})
    db.t2.createIndex({id:1})
    db.t2.createIndex({name:1})
    
    db.createUser({user:'ceshi',pwd:'c123456', roles:[{role:'readWrite', db:'czg'}]})

    1、mongoexport --help 命令详解

    mongoexport
    general options:
          --help                                                打印帮助信息
          --version                                             打印版本号
    
    verbosity options:
      -v, --verbose=<level>                                     增加备份过程中日志详细程序,例如 -vvvv 打的日志最多
          --quiet                                               备份过程中不输出日志
    
    connection options:
      -h, --host=<hostname>                                     连接地址 (setname/host1,host2 for replica sets)
          --port=<port>                                         端口号 可以 --host hostname:port(can also use --host hostname:port)
    
    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
          --sslFIPSMode                                         use FIPS mode of the installed openssl library
          --tlsInsecure                                         bypass the validation for server's certificate chain and host name
    
    authentication options:
      -u, --username=<username>                                 用户名
      -p, --password=<password>                                 密码
          --authenticationDatabase=<database-name>              指定验证库
          --authenticationMechanism=<mechanism>                 指定验证机制
    
    kerberos options:
          --gssapiServiceName=<service-name>                    service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
          --gssapiHostName=<host-name>                          hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's
                                                                address>)
    
    uri options:
          --uri=mongodb-uri                                     mongodb uri 连接信息
    
    namespace options:
      -d, --db=<database-name>                                  导出库名称
      -c, --collection=<collection-name>                        导出集合名称
              
    uri options:          
          --uri=mongodb-uri                                     mongodb uri 连接信息
              
    output options:          
      -f, --fields=<field>[,<field>]*                           指定一个或多个列进行导出 -f "name,age"
          --fieldFile=<filename>                                --fields的替代参数,指定多个列到文件中,文件必须以0x0A结尾,仅对-type=csv时有效
          --type=<type>                                         输出格式,默认为json格式 (defaults to 'json')
      -o, --out=<filename>                                      输出文件,如果未指定,输出到当前屏幕
          --jsonArray                                           导出结果全部写入到单个json数组中,默认情况下,每条记录是一个json
          --pretty                                              以易读的格式输出文档。
          --noHeaderLine                                        在输出CSV格式时,第一行都是字段名称,使用这个参数后,将不写入字段名称
          --jsonFormat=<type>                                   提定输出模式为规范模式或宽松模式,默认为宽松模式
              
    querying options:          
      -q, --query=<json>                                        指定查询条件进行过滤, '{x:{$gt:1}}'
          --queryFile=<filename>                                指定查询文件进行过滤 (JSON)
      -k, --slaveOk                                             3.2版本后已弃用 (default true)
          --readPreference=<string>|<json>                      读偏好设置
          --forceTableScan                                      force a table scan (do not use $snapshot or hint _id). Deprecated since this is default
                                                                behavior on WiredTiger
          --skip=<count>                                        控制从哪里开始导出数据
          --limit=<count>                                       限制导出条数
          --sort=<json>                                         导出结果进行排序,结果集必须小于32M e.g. '{x:1}'
          --assertExists                                        if specified, export fails if the collection does not exist

    示例:

    1.1 备份集合,默认为json文件

    mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 -o /root/mongo_export/czg_t2.json

    1.2 备份集合,存储为csv 文件,必须要指定导出列

    mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 --type=csv --fields=id,name -o /root/mongo_export/czg_t2.csv

    1.3 如果列比较多,保存成为一个文件,指定文件中的列进行导出

    root@ip-172-31-30-45:~/mongo_export# cat fields.txt
    id
    name
    ----------------------分割线----------------------
    mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 --type=csv --fieldFile=/root/mongo_export/fields.txt -o /root/mongo_export/czg_t2.csv

    1.4 导出的csv文件默认带字段名称,使用--noHeaderLine 进行不显示字段名

    mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 --type=csv --fields=id,name --noHeaderLine -o /root/mongo_export/czg_t2.csv

    1.5 以uri信息连接数据库,导出数据

    mongoexport --uri="mongodb://root:c123456@127.0.0.1:27017/czg?authsource=admin" -c t2 -o /root/mongo_export/czg_t2.json

    1.6 以过滤条件导出数据

    mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 -q='{"id":{"$gte":50}}' -o /root/mongo_export/czg_t2.json

    1.7 限制导出条数

    mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 -q='{"id":{"$gte":50}}' --limit=10 -o /root/mongo_export/czg_t2.json

    2、

    1、mongoexport --help 命令详解

    general options:
          --help                                                打印帮助信息
          --version                                             打印版本号
    
    verbosity options:
      -v, --verbose=<level>                                     增加备份过程中日志详细程序,例如 -vvvv 打的日志最多
          --quiet                                               备份过程中不输出日志
    
    connection options:
      -h, --host=<hostname>                                     连接地址 (setname/host1,host2 for replica sets)
          --port=<port>                                         端口号 可以 --host hostname:port(can also use --host hostname:port)
    
    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
          --sslFIPSMode                                         use FIPS mode of the installed openssl library
          --tlsInsecure                                         bypass the validation for server's certificate chain and host name
    
    authentication options:
      -u, --username=<username>                                 用户名
      -p, --password=<password>                                 密码
          --authenticationDatabase=<database-name>              指定验证库
          --authenticationMechanism=<mechanism>                 指定验证机制
    
    kerberos options:
          --gssapiServiceName=<service-name>                    service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
          --gssapiHostName=<host-name>                          hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's
                                                                address>)
    
    uri options:
          --uri=mongodb-uri                                     mongodb uri 连接信息
    
    namespace options:
      -d, --db=<database-name>                                  导出库名称
      -c, --collection=<collection-name>                        导出集合名称
              
    uri options:          
          --uri=mongodb-uri                                     mongodb uri 连接信息
              
    output options:          
      -f, --fields=<field>[,<field>]*                           指定一个或多个列进行导出 -f "name,age"
          --fieldFile=<filename>                                --fields的替代参数,指定多个列到文件中,文件必须以0x0A结尾,仅对-type=csv时有效
          --type=<type>                                         输出格式,默认为json格式 (defaults to 'json')
      -o, --out=<filename>                                      输出文件,如果未指定,输出到当前屏幕
          --jsonArray                                           导出结果全部写入到单个json数组中,默认情况下,每条记录是一个json
          --pretty                                              以易读的格式输出文档。
          --noHeaderLine                                        在输出CSV格式时,第一行都是字段名称,使用这个参数后,将不写入字段名称
          --jsonFormat=<type>                                   提定输出模式为规范模式或宽松模式,默认为宽松模式
              
    querying options:          
      -q, --query=<json>                                        指定查询条件进行过滤, '{x:{$gt:1}}'
          --queryFile=<filename>                                指定查询文件进行过滤 (JSON)
      -k, --slaveOk                                             3.2版本后已弃用 (default true)
          --readPreference=<string>|<json>                      读偏好设置
          --forceTableScan                                      force a table scan (do not use $snapshot or hint _id). Deprecated since this is default
                                                                behavior on WiredTiger
          --skip=<count>                                        控制从哪里开始导出数据
          --limit=<count>                                       限制导出条数
          --sort=<json>                                         导出结果进行排序,结果集必须小于32M e.g. '{x:1}'
          --assertExists                                        if specified, export fails if the collection does not exist

    2.1 将 czg_t2.json 文件导入到 czg库,tt2集合中

    mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2 /root/mongo_export/czg_t2.json

    2.2 将 czg_t2.json 文件导入到 czg库,tt2集合中,如果遇到冲突,则覆盖写

    mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2 --mode=upsert /root/mongo_export/czg_t2.json

    2.3 将czg_t2.csv 文件导入到 czg库,tt2_csv集合中,导入时必须指定列名称 (如果备份文件第一行是列名称,也会被当成数据导入到数据库中)

    mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2_csv --type=csv --fields=id,name /root/mongo_export/czg_t2.csv

    2.4 将czg_t2.csv 文件导入到 czg库,tt2_csv集合中,使用 --headerline 参数,使用csv文件第一行做为字段名称

    mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2_csv --type=csv --headerline /root/mongo_export/czg_t2.csv

    2.5 不带-c参数导入数据,会使用文件名做为集合名,导入后集合名称为 czg_t2

    mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg --type=csv --headerline /root/mongo_export/czg_t2.csv

    2.6 导入数据时,指定字段类型进行导入 (注意:csv文件第一行不要有字段名称)

    mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2_types --type=csv --columnsHaveTypes --fields="id.int32(),name.string()" /root/mongo_export/czg_t2.csv

    2.7 导入数据时,忽略掉csv文件中的空白字段

    创建一些测试数据
    root@ip-172-31-30-45:~/mongo_export# cat test.txt
    id,name,age
    1,,
    2,chai,10
    3,,20
    
    ----------------分割线-------------------
    mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t_ignore --type=csv --fields=id,name --ignoreBlanks /root/mongo_export/test.txt
  • 相关阅读:
    java基础多线程
    java反射基础
    JSP-4(Session)
    JSP-3
    JSP-2
    复试计算机专业文献翻译
    jsp
    实现输入输出对应模型
    servlet
    tomcat的入门(1)
  • 原文地址:https://www.cnblogs.com/nanxiang/p/15292649.html
Copyright © 2011-2022 走看看