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

news2024/9/25 13:20:48

在一次数据更新中,同事把老数据进行了清空操作,但是新的逻辑数据由于某种原因(好像是她的电脑中病毒了),一直无法正常连接数据库进行数据插入,然后下午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

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

D:\MongoDB\Server\5.0\bin>mongoimport.exe -h localhost:28007 -u admin -p 123456 -d database -c result --file D:/project/result.json
2023-04-11T13:34:39.799+0800    connected to: mongodb://localhost:28007/
2023-04-11T13:34:42.799+0800    [#######.................] database.result 20.2MB/66.4MB (30.4%)
2023-04-11T13:34:45.799+0800    [##############..........] database.result 40.5MB/66.4MB (61.1%)
2023-04-11T13:34:48.799+0800    [#####################...] database.result 60.4MB/66.4MB (91.0%)
2023-04-11T13:34:49.660+0800    [########################] database.result 66.4MB/66.4MB (100.0%)
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 进行参数意思的查看

D:\MongoDB\Server\5.0\bin>mongoimport --help
Usage:
  mongoimport <options> <connection-string> <file>

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

Connection strings must begin with mongodb:// or mongodb+srv://.

See http://docs.mongodb.com/database-tools/mongoimport/ for more information.

general options:
      /help                                       print usage
      /version                                    print the tool version and exit
      /config:                                    path to a configuration file

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)

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>                        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
      /awsSessionToken:<aws-session-token>        session token to authenticate via AWS IAM

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>)

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 (default: stop)
      /type:<type>                                input format to import: json, csv, or tsv
      /columnsHaveTypes                           indicates 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,
                                                  boolean, date, date_go, date_ms, date_oracle, decimal, 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)
      /legacy                                     use the legacy extended JSON format
      /useArrayIndexFields                        indicates that field names may include array indexes that should be
                                                  used to construct arrays during import (e.g. foo.0,foo.1). Indexes
                                                  must start from 0 and increase sequentially (foo.1,foo.0 would fail).

ingest options:
      /drop                                       drop collection before inserting documents
      /ignoreBlanks                               ignore fields with empty values in CSV and TSV
      /maintainInsertionOrder                     insert the documents in the order of their appearance in the input
                                                  source. By default the insertions will be performed in an arbitrary
                                                  order. Setting this flag also enables the behavior of --stopOnError
                                                  and restricts NumInsertionWorkers to 1.
  /j, /numInsertionWorkers:<number>               number of insert operations to run concurrently
      /stopOnError                                halt after encountering any error during importing. By default,
                                                  mongoimport will attempt to continue through document validation and
                                                  DuplicateKey errors, but with this option enabled, the tool will stop
                                                  instead. A small number of documents may be inserted after
                                                  encountering an error even with this option enabled; use
                                                  --maintainInsertionOrder to halt immediately after an error
      /mode:[insert|upsert|merge|delete]          insert: insert only, skips matching documents. upsert: insert new
                                                  documents or replace existing documents. merge: insert new documents
                                                  or modify existing documents. delete: deletes matching documents
                                                  only. If upsert fields match more than one document, only one
                                                  document is deleted. (default: 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/899875.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【图论】Floyd算法

一.简介 Floyd算法&#xff0c;也称为Floyd-Warshall算法&#xff0c;是一种用于解决所有节点对最短路径问题的动态规划算法。它可以在有向图或带权图中找到任意两个节点之间的最短路径。 Floyd算法的基本思想是通过中间节点逐步优化路径长度。它使用一个二维数组来存储任意两…

挖漏洞竟能赚取百万美金?来认识一下这 6 位百万美元白帽黑客

在黑客世界&#xff0c;有三种人&#xff1a;白帽&#xff08;黑客&#xff09;、灰帽&#xff08;黑客&#xff09;和黑帽&#xff08;黑客&#xff09;。其中&#xff0c;白帽黑客&#xff0c;即 White Hat Hacker&#xff0c;又称白帽子&#xff0c;它们用自己的黑客技术来维…

redux的介绍、安装、三大核心与执行流程

redux的介绍、安装、三大核心与执行流程 一、redux的基本介绍二、redux的安装三、redux核心概念3.1 action3.2 reducer3.3 store 四、Redux代码执行流程五、加减案例练习 一、redux的基本介绍 redux中文官网Redux 是 React 中最常用的状态管理工具&#xff08;状态容器&#x…

B树和B+树MySQL为什么用B+树?

文章目录 B树和B树B树B树的定义B树的插入操作删除操作 B树B树的定义B树的插入操作删除操作 B树和B树的区别?MySQL数据库为啥用B树作为索引&#xff0c;而不用B树? B树和B树 原文链接&#xff1a;https://blog.csdn.net/jinking01/article/details/115130286 B树 B树的定义…

深入理解python虚拟机:程序执行的载体——栈帧

栈帧&#xff08;Stack Frame&#xff09;是 Python 虚拟机中程序执行的载体之一&#xff0c;也是 Python 中的一种执行上下文。每当 Python 执行一个函数或方法时&#xff0c;都会创建一个栈帧来表示当前的函数调用&#xff0c;并将其压入一个称为调用栈&#xff08;Call Stac…

RT1052的EPWM

文章目录 1 EPWM介绍1.1 引脚1.2 时钟1.3 比较寄存器 2 函数 1 EPWM介绍 RT1052 具有 4 个 eFlexPWM(eFlexWM1~eFlex_PWM4)。 每个 eFlexPWM 可以产生四路互补 PWM即产生 8 个 PWM&#xff0c;也可以产生相互独立的 PWM 波。四路分别是模块0-3每个 eFlexPWM 具有各自的故障检…

如何学习专业的学术用语01

问题的提出——凭啥人家写的词汇这么专业 做法一 做法二&#xff1a;做一个专业数据库 专门做教育技术类的

换过3个工作,我却得出10年测试人的血泪经验

我跟大多数IT职场的测试新人起点差不多&#xff0c;在测试的这条路上&#xff0c;没有天生的聪明天资&#xff0c;也没有一个耀眼的学历。在北京这样一个随便一个同事不是清华的本硕&#xff0c;就是北邮北航的硕士下&#xff0c;自己也常常感到惭愧。 自己从事测试多年&#…

论文笔记 Graph Attention Networks

2018 ICLR 1 intro 1.1. GCN的不足 无法完成inductive任务 inductive任务是指&#xff1a; 训练阶段与测试阶段需要处理的graph不同。通常是训练阶段只是在子图上进行&#xff0c;测试阶段需要处理未知的顶点。GGN 的参数依赖于邻接矩阵A/拉普拉斯矩阵L&#xff0c;所以换了…

一个完整挖洞 /src 漏洞实战流程【渗透测试】

目录: 1.如何找漏洞 2.找到后如何挖漏洞 3.漏洞如何提交 只要搞渗透&#xff0c;不就会听到很多行业内人前辈一直在重复:“信息搜集” 信息搜集有多重要&#xff0c;你搜集的到的多少资产信息&#xff0c;决定了你后续进行的一系列实战到什么程度! 要说 SQL 注入的漏洞咋找…

【博客700】如何使用 Nginx Ingress 快速实现金丝雀与蓝绿部署

如何使用 Nginx Ingress 快速实现金丝雀与蓝绿部署 背景 越来越多的应用采用微服务架构&#xff0c;应用数量相比传统模式更多&#xff0c;管理更加复杂&#xff0c;发布更加频繁&#xff0c;如果直接将新版本上线发布给全部用户。一旦遇到线上事故&#xff08;或BUG&#xff…

Selenium的使用:WEB功能测试

Selenium是ThrougthWorks公司一个强大的开源WEB功能测试工具系列&#xff0c;本系统包括多款软件 Selenium语言简单&#xff0c;用(Command,target,value)三种元素组成一个行为&#xff0c;并且有协助录制脚本工具&#xff0c;但Selenese有一些严格的限制&#xff1a; …

在字节和滴滴划水四年,过于真实了...

先简单交代一下&#xff0c;我是某不知名211的计算机本硕&#xff0c;18年毕业加入滴滴&#xff0c;之后跳槽到了头条&#xff0c;一直从事测试开发相关的工作。之前没有实习经历&#xff0c;算是四年半的工作经验吧。 这四年半之间完成了一次晋升&#xff0c;换了一家公司&am…

torch.cuda.is_available()为false的解决办法

一、问题 在进行torch进行开发的过程中&#xff0c;我们习惯性的会使用pip install torch这样的方式来安装torch的包。 其实这样的是安装CPU的torch。 在导入包&#xff0c;执行下面代码的过程中&#xff0c;会出现结果为false。 import torchprint(torch.cuda.is_availabl…

04-数据集汇总

一、3D检测数据集 1、Argoverse数据集[参考] 年份&#xff1a;2019年&#xff1b; 作者&#xff1a;Argo AI等&#xff1b; 场景数&#xff1a;共113个场景&#xff0c;室外&#xff0c;包括USA&#xff0c;Pennsylvania&#xff0c;Miami&#xff0c;Florida等&#xff1b…

Django进阶:DRF(Django REST framework)

什么是DRF&#xff1f; DRF即Django REST framework的缩写&#xff0c;官网上说&#xff1a;Django REST framework是一个强大而灵活的工具包&#xff0c;用于构建Web API。 简单来说&#xff1a;通过DRF创建API后&#xff0c;就可以通过HTTP请求来获取、创建、更新或删除数据(…

CFDEM-OpenFOAM-Yade安装教程

在网上搜索与OpenFOAM相关的颗粒两相流计算资料时&#xff0c;发现了一个CFD-DEM coupled simulations with Yade and OpenFOAM。 在此之前&#xff0c;我学习过OpenFOAM自带的颗粒计算求解器&#xff0c;但是自带的求解器有很多缺点&#xff0c;最大的缺点就是颗粒运动方程的求…

10. 实现业务功能--退出登录

目录 1. 实现 Controller 2. 单体测试 3. 实现前端界面 退出的具体实现逻辑如下&#xff1a; 1. 用户访问退出接口 2. 服务器注销 Session( 在 Controller 中可以直接进行处理 &#xff09; 3. 返回成功或失败 4. 如果返回成功浏览器跳转到相应页面 5. 结束 一般来说&#…

Python入门--开发工具

Python是一种优秀的编程语言&#xff0c;具有简单易学、开放源代码、高效可靠等特点&#xff0c;广泛应用于Web开发、科学计算、数据分析、人工智能等领域。以下是常用的Python开发工具&#xff1a; PyCharm&#xff1a;JetBrains公司开发的Python IDE&#xff0c;功能强大&…

VMware vSphere Client端设置热添加虚拟机的CPU和内存

使用vSphere Client连接到VMware ESXi Server&#xff0c;在“配置→网络”中&#xff0c;可以看到&#xff0c;当前有两个虚拟交换机&#xff0c;并且为该虚拟交换机分配了管理地址10.10.228.81&#xff0c;点击“添加网络”如图所示。 添加配置向导&#xff0c;在网络类型&am…