美文网首页
【mongo】mongoDB export/import数据导

【mongo】mongoDB export/import数据导

作者: Bogon | 来源:发表于2021-11-22 07:25 被阅读0次

    1.备份恢复工具介绍:

    mongoexport/mongoimport
    mongodump/mongorestore

    2.备份工具区别在哪里?

    mongoexport/mongoimport 导入/导出的是JSON格式或者CSV格式。
    mongodump/mongorestore 导入/导出的是BSON格式。

    JSON可读性强但体积较大,BSON则是二进制文件,体积小但对人类几乎没有可读性。

    2.3

    在一些mongodb版本之间,BSON格式可能会随版本不同而有所不同,所以不同版本之间用mongodump/mongorestore可能不会成功,具体要看版本之间的兼容性。
    当无法使用BSON进行跨版本的数据迁移的时候,使用JSON格式即mongoexport/mongoimport是一个可选项。
    跨版本的mongodump/mongorestore个人并不推荐,实在要做请先检查文档看两个版本是否兼容。

    2.4

    JSON虽然具有较好的跨版本通用性,但其只保留了数据部分,不保留索引,账户等其他基础信息。使用时应该注意。

    2.5

    mongoexport不支持普通导出单个db的所有的collection
    mongodump支持普通导出单个db的所有的collection

    3.应用场景总结:

    mongoexport/mongoimport

    1、异构平台迁移 mysql <---> mongodb
    2、同平台,跨大版本:mongodb2.x ---> mongodb3.x

    mongodump/mongorestore
    日常备份恢复时使用.

    mongoDB中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。

    你可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。

     mongoexport --help 
    
    image.png
    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)
    
    authentication options:
      -u, --username=<username>                       username for authentication
      -p, --password=<password>                       password for authentication
          --authenticationDatabase=<database-name>    database that holds the user's 
    
    namespace options:
      -d, --db=<database-name>                        database to use
      -c, --collection=<collection-name>              collection to use
    
    output options:
      -f, --fields=<field>[,<field>]*                 comma separated list of field names (required for exporting CSV)
                                                      e.g. -f "name,age"
          --fieldFile=<filename>                      file with field names - 1 per line
          --type=<type>                               the output format, either json or csv (defaults to 'json') (default:
                                                      json)
      -o, --out=<filename>                            output file; if not specified, stdout is used
          --jsonArray                                 output to a JSON array rather than one object per line
          --pretty                                    output JSON formatted to be human-readable
          --noHeaderLine                              export CSV data without a list of field names at the first line
          --jsonFormat=<type>                         the extended JSON format to output, either canonical or relaxed
                                                      (defaults to 'relaxed') (default: relaxed)
    
    querying options:
      -q, --query=<json>                              query filter, as a JSON string, e.g., '{x:{$gt:1}}'
          --queryFile=<filename>                      path to a file containing a query filter (JSON)
      -k, --slaveOk                                   allow secondary reads if available (default true) (default: false)
          --readPreference=<string>|<json>            specify either a preference mode (e.g. 'nearest') or a preference
                                                      json object (e.g. '{mode: "nearest", tagSets: [{a: "b"}],
                                                      maxStalenessSeconds: 123}')
          --forceTableScan                            force a table scan (do not use $snapshot or hint _id). Deprecated
                                                      since this is default behavior on WiredTiger
          --skip=<count>                              number of documents to skip
          --limit=<count>                             limit the number of documents to export
          --sort=<json>                               sort order, as a JSON string, e.g. '{x:1}'
          --assertExists                              if specified, export fails if the collection does not exist
                                                      (default: false)
    
    
    mongoexport   \
      -h xx.xx.xx.xx \
      --port=27017  \
      --username=username \
      --password=passwd \
      --authenticationDatabase=admin  \
      -d  testDB  \
      -c  testCol \
      -q '{"$and":[{"time":{"$gt": new Date(1626307200000)}},{"time":{"$lt": new Date(1626534000000)}}]}' \
      --type json \
      -o testDB_testCol.json
    
    mongoexport \
      -h 127.0.0.1 \
      --port=27021   \
      --username=username \
      --password="passwd"   \
      --authenticationDatabase=admin  \
      -d  testDB  \
      -c  testCol \
      -q '{"title":{$in:["部门群解绑","部门群升级","部门群创建"]}}'    \
      --type  json   \
      -o testDB_testCol.json.json
    
    /path/to/mongoexport  \
      -h 127.0.0.1 \
      --port=27017   \
      --username=username \
      --password='passwd'   \
      --authenticationDatabase=admin  \
      -d testDB \
      -c testCol  \
      -q  "{customFold: 1}" \
      -f  groupId,userId \
       --type  csv  \
      -o  testDB_testCol_groupId_userId.csv
    

    mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。

    该工具可以导入JSON格式数据,也可以导入CSV格式数据。

    image.png
    mongoimport \
      --host 192.168.1.21 \
      --port 8635 \
      -u  username \
      -p 'passwd' \
      --authenticationDatabase admin \
      --ssl --sslAllowInvalidCertificates \
      --type json   \
      --db testDB \
      --collection testCol \
      --file /path/to/exportFile.json
    

    如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式。

    mongoimport  \
      --host 192.168.1.21 \
      --port 8635 \
      -u  username \
      -p 'passwd' \
      --authenticationDatabase admin \
      -d testDB \
      -c testCol \
      --type csv \
      --headerline \
      --file /path/to/exportFile.csv
    
    

    注意: -headerline:指明第一行是列名,不需要导入

    如果提供的js脚本是带insert的语句,需要登陆mongo shell,使用load()函数执行

    $mongo  --host xx.xx.xx.xx --port=27017 -u username --password='passwd' --authenticationDatabase=admin
    > use testDB;
    > load("/path/to/test.js");
    

    参考

    mongoexport 带条件导出数据
    https://my.oschina.net/trydaydayup/blog/876015

    MongoDB备份(mongoexport)与恢复(mongoimport)
    https://zhuanlan.zhihu.com/p/343561627

    mongoexport synopsis
    https://www.docs4dev.com/docs/zh/mongodb/v3.6/reference/reference-program-mongoimport.html

    MongoDB - mongoexport 嵌套数组中的所有对象
    https://www.coder.work/article/531149

    monoDB 连接字符串 URI 格式
    https://www.docs4dev.com/docs/zh/mongodb/v3.6/reference/reference-connection-string.html

    相关文章

      网友评论

          本文标题:【mongo】mongoDB export/import数据导

          本文链接:https://www.haomeiwen.com/subject/asputrtx.html