当前位置:   article > 正文

mongodb.使用自带命令工具导出导入数据_mongoexport

mongoexport

在一次数据更新中,同事把老数据进行了清空操作,但是新的逻辑数据由于某种原因(好像是她的电脑中病毒了),一直无法正常连接数据库进行数据插入,然后下午2点左右要给甲方演示,所以要紧急恢复本地的部分数据到生产库。

在此之前我只用过 mongo 自带的命令 mongoexport 进行过导出操作,把数据库的某个 collection 导出为 json 文件,那么这次是要先导出再导入,实现了一个完整的数据迁移闭环,所以在此记录一下,以备不时之需。

一、下载 mongo 工具包

mongo工具包包括管理数据的一些工具 exe 文件,具体如下:

  • mongoexport.exe:导出数据命令工具
  • mongoimport.exe:导入数据命令工具
  • bsondump.exe: 用于将导出的BSON文件格式转换为JSON格式
  • mongodump.exe: 用于从mongodb数据库中导出BSON格式的文件,类似于mysql的dump工具mysqldump
  • mongofiles.exe: 用于和mongoDB的GridFS文件系统交互的命令,并可操作其中的文件,它提供了我们本地系统与GridFS文件系统之间的存储对象接口
  • mongorestore.exe: 用于恢复导出的BSON文件到 mongodb 数据库中
  • mongostat.exe: 当前 mongod 状态监控工具,像linux中监控linux的vmstat
  • mongotop.exe: 提供了一个跟踪mongod数据库花费在读写数据的时间,为每个collection都会记录,默认记录时间是按秒记录

这个工具跟 mongo 的版本有关系,部分版本自带该工具包,比如下图的 4.x 版本,我用的 5.0 版本没有自带工具包,所以我需要先去官网下载工具包文件,然后把 bin 目录下的工具复制到 5.0 版本的 bin 目录下,才能进行数据的导出、导入操作。
工具包的下载地址为:mongo工具包下载地址,解压后把bin文件夹里的文件全部拷贝到 MongoDB 安装目录bin文件夹下。

二、导出数据

进入到 mongo 的安装目录 bin 下,使用 mongoexport 工具进行数据的 导出 操作

1、无密码导出操作:

mongoexport.exe -h localhost:28007 -d database  -c result -o D:/project/result.json

2、有密码的导出操作:

mongoexport.exe -h localhost:28007 -d database -u admin  -p 123456  -c result -o D:/project/result.json

三、导入数据

进入到 mongo 的安装目录 bin 下,使用 mongoimport 工具进行数据的 导入 操作

mongoimport.exe -h localhost:28007 -u admin -p 123456 -d database -c result --file D:/project/result.json

执行结果如下表示导入成功

  1. D:\MongoDB\Server\5.0\bin>mongoimport.exe -h localhost:28007 -u admin -p 123456 -d database -c result --file D:/project/result.json
  2. 2023-04-11T13:34:39.799+0800 connected to: mongodb://localhost:28007/
  3. 2023-04-11T13:34:42.799+0800 [#######.................] database.result 20.2MB/66.4MB (30.4%)
  4. 2023-04-11T13:34:45.799+0800 [##############..........] database.result 40.5MB/66.4MB (61.1%)
  5. 2023-04-11T13:34:48.799+0800 [#####################...] database.result 60.4MB/66.4MB (91.0%)
  6. 2023-04-11T13:34:49.660+0800 [########################] database.result 66.4MB/66.4MB (100.0%)
  7. 2023-04-11T13:34:49.660+0800 386810 document(s) imported successfully. 0 document(s) failed to import.

参数释义:
-h :指的是 host 主机地址
-u :指的是用户账号
-p :指的是账户密码
-d :指的是数据库 database 简称
-c :指的是表 collection 简称
-o :指的是导出路径 output 简称
--file :指的是需要导入的文件

四、其他

使用过程中可以使用 --help 进行参数意思的查看

  1. D:\MongoDB\Server\5.0\bin>mongoimport --help
  2. Usage:
  3. mongoimport <options> <connection-string> <file>
  4. Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.
  5. Connection strings must begin with mongodb:// or mongodb+srv://.
  6. See http://docs.mongodb.com/database-tools/mongoimport/ for more information.
  7. general options:
  8. /help print usage
  9. /version print the tool version and exit
  10. /config: path to a configuration file
  11. verbosity options:
  12. /v, /verbose:<level> more detailed log output (include multiple times for more verbosity,
  13. e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)
  14. /quiet hide all log output
  15. connection options:
  16. /h, /host:<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
  17. /port:<port> server port (can also use --host hostname:port)
  18. ssl options:
  19. /ssl connect to a mongod or mongos that has ssl enabled
  20. /sslCAFile:<filename> the .pem file containing the root certificate chain from the
  21. certificate authority
  22. /sslPEMKeyFile:<filename> the .pem file containing the certificate and key
  23. /sslPEMKeyPassword:<password> the password to decrypt the sslPEMKeyFile, if necessary
  24. /sslCRLFile:<filename> the .pem file containing the certificate revocation list
  25. /sslFIPSMode use FIPS mode of the installed openssl library
  26. /tlsInsecure bypass the validation for server's certificate chain and host name
  27. authentication options:
  28. /u, /username:<username> username for authentication
  29. /p, /password:<password> password for authentication
  30. /authenticationDatabase:<database-name> database that holds the user's credentials
  31. /authenticationMechanism:<mechanism> authentication mechanism to use
  32. /awsSessionToken:<aws-session-token> session token to authenticate via AWS IAM
  33. kerberos options:
  34. /gssapiServiceName:<service-name> service name to use when authenticating using GSSAPI/Kerberos
  35. (default: mongodb)
  36. /gssapiHostName:<host-name> hostname to use when authenticating using GSSAPI/Kerberos (default:
  37. <remote server's address>)
  38. namespace options:
  39. /d, /db:<database-name> database to use
  40. /c, /collection:<collection-name> collection to use
  41. uri options:
  42. /uri:mongodb-uri mongodb uri connection string
  43. input options:
  44. /f, /fields:<field>[,<field>]* comma separated list of fields, e.g. -f name,age
  45. /fieldFile:<filename> file with field names - 1 per line
  46. /file:<filename> file to import from; if not specified, stdin is used
  47. /headerline use first line in input source as the field list (CSV and TSV only)
  48. /jsonArray treat input source as a JSON array
  49. /parseGrace:<grace> controls behavior when type coercion fails - one of: autoCast,
  50. skipField, skipRow, stop (default: stop)
  51. /type:<type> input format to import: json, csv, or tsv
  52. /columnsHaveTypes indicates that the field list (from --fields, --fieldsFile, or
  53. --headerline) specifies types; They must be in the form of
  54. '<colName>.<type>(<arg>)'. The type can be one of: auto, binary,
  55. boolean, date, date_go, date_ms, date_oracle, decimal, double, int32,
  56. int64, string. For each of the date types, the argument is a datetime
  57. layout string. For the binary type, the argument can be one of:
  58. base32, base64, hex. All other types take an empty argument. Only
  59. valid for CSV and TSV imports. e.g. zipcode.string(),
  60. thumbnail.binary(base64)
  61. /legacy use the legacy extended JSON format
  62. /useArrayIndexFields indicates that field names may include array indexes that should be
  63. used to construct arrays during import (e.g. foo.0,foo.1). Indexes
  64. must start from 0 and increase sequentially (foo.1,foo.0 would fail).
  65. ingest options:
  66. /drop drop collection before inserting documents
  67. /ignoreBlanks ignore fields with empty values in CSV and TSV
  68. /maintainInsertionOrder insert the documents in the order of their appearance in the input
  69. source. By default the insertions will be performed in an arbitrary
  70. order. Setting this flag also enables the behavior of --stopOnError
  71. and restricts NumInsertionWorkers to 1.
  72. /j, /numInsertionWorkers:<number> number of insert operations to run concurrently
  73. /stopOnError halt after encountering any error during importing. By default,
  74. mongoimport will attempt to continue through document validation and
  75. DuplicateKey errors, but with this option enabled, the tool will stop
  76. instead. A small number of documents may be inserted after
  77. encountering an error even with this option enabled; use
  78. --maintainInsertionOrder to halt immediately after an error
  79. /mode:[insert|upsert|merge|delete] insert: insert only, skips matching documents. upsert: insert new
  80. documents or replace existing documents. merge: insert new documents
  81. or modify existing documents. delete: deletes matching documents
  82. only. If upsert fields match more than one document, only one
  83. document is deleted. (default: insert)
  84. /upsertFields:<field>[,<field>]* comma-separated fields for the query part when --mode is set to
  85. upsert or merge
  86. /writeConcern:<write-concern-specifier> write concern options e.g. --writeConcern majority, --writeConcern
  87. '{w: 3, wtimeout: 500, fsync: true, j: true}'
  88. /bypassDocumentValidation bypass document validation
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/814725
推荐阅读
相关标签
  

闽ICP备14008679号