`
san_yun
  • 浏览: 2639096 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

mongoDB 数据备份文档

 
阅读更多

参考:http://docs.mongodb.org/manual/administration/import-export/

mporting and Exporting MongoDB Data

Full database instance backups are useful for disaster recovery protection and routine database backup operation; however, some cases require additional import and export functionality.

This document provides an overview of the import and export tools provided in distributions for MongoDB administrators. These utilities are useful when you want to backup or export a portion of your database without capturing the state of the entire database. For more complex data migration tasks, you may want to write your own import and export scripts using a client driver to interact with the database itself.

 

Collection Export with mongoexport

With the mongoexport utility you can create a backup file. In the most simple invocation, the command takes the following form:

mongoexport --collection collection --out collection.json
 

 

This will export all documents in the collection named collection into the file collection.json . Without the output specification (i.e. “--out collection.json ”,) mongoexport writes output to standard output (i.e. “stdout.”) You can further narrow the results by supplying a query filter using the “--query ” and limit results to a single database using the “--db ” option. For instance:

mongoexport --db sales --collection contacts --query '{"field": 1}'
 

This command returns all documents in the sales database’s contacts collection, with a field named field with a value of 1 . Enclose the query in single quotes (e.g. ' ) to ensure that it does not interact with your shell environment. The resulting documents will return on standard output.

By default, mongoexport returns one JSON document per MongoDB document. Specify the “--jsonArray ” argument to return the export as a single JSON array. Use the “--csv ” file to return the result in CSV (comma separated values) format.

If your mongod instance is not running, you can use the “--dbpath ” option to specify the location to your MongoDB instance’s database files. See the following example:

mongoexport --db sales --collection contacts --dbpath /srv/MongoDB/
 

This reads the data files directly. This locks the data directory to prevent conflicting writes. The mongod process must not be running or attached to these data files when you run mongoexport in this configuration.

The “--host ” and “--port ” options allow you to specify a non-local host to connect to capture the export. Consider the following example:

mongoexport --host mongodb1.example.net --port 37017 --username user --password pass --collection contacts --file mdb1-examplenet.json
 

On any mongoexport command you may, as above specify username and password credentials as above.

Collection Import with mongoimport

To restore a backup taken with mongoexport . Most of the arguments to mongoexport also exist for mongoimport . Consider the following command:

mongoimport --collection collection --file collection.json
 

This imports the contents of the file collection.json into the collection named collection . If you do not specify a file with the “--file ” option, mongoimport accepts input over standard input (e.g. “stdin.”)

If you specify the “--upsert ” option, all of mongoimport operations will attempt to update existing documents in the database and insert other documents. This option will cause some performance impact depending on your configuration.

You can specify the database option “--db ” to import these documents to a particular database. If your MongoDB instance is not running, use the “--dbpath ” option to specify the location of your MongoDB instance’s database files. Consider using the “--journal ” option to ensure that mongoimport records its operations in the journal. The mongod process must not be running or attached to these data files when you run mongoimport in this configuration.

Use the “--ignoreBlanks ” option to ignore blank fields. For CSV and TSV imports, this option provides the desired functionality in most cases: it avoids inserting blank fields in MongoDB documents.

分享到:
评论

相关推荐

    mongodb 备份恢复工具

    **MongoDB数据同步与增量备份.pptx 和 MongoDB数据同步.pptx** 可能是关于如何执行MongoDB数据同步和增量备份的演示文稿,可能包含了具体的案例和操作流程,对于理解相关概念和操作流程非常有价值。 **tokusync** ...

    mongodb3.2.4说明文档

    与传统的表格型数据存储方式不同,MongoDB 中的数据是以 BSON(Binary JSON)格式存储的文档,这些文档类似于 JSON 对象,便于存储复杂的数据类型。文档存储的特点使得 MongoDB 在处理半结构化或非结构化数据时表现...

    MongoDB基础教学文档

    8. **命令行工具**:MongoDB提供了一系列命令行工具,如`mongo` shell,用于交互式地连接和管理数据库,以及`mongodump`和`mongorestore`用于数据备份和恢复。 9. **驱动程序与连接**:MongoDB有多种编程语言的驱动...

    mongodb备份

    由于其数据存储的重要性,定期备份是任何 MongoDB 部署的关键组成部分。以下是对 MongoDB 备份命令的详细解释,以及如何使用 JSON 和 CSV 格式进行数据导出和导入。 1. **MongoDB 备份命令**: MongoDB 提供了两种...

    mongoDB LINUX增量备份

    MongoDB提供了`mongodump`和`mongorestore`命令行工具来进行数据备份与恢复。`mongodump`用于生成数据库的二进制备份,而`mongorestore`则用于从这些备份中恢复数据。 1. **基础全量备份**:执行全量备份是创建增量...

    mongodb分片备份

    MongoDB 是一个高性能、开源的文档型数据库,广泛应用于各种规模的企业和项目中。在大型部署中,为了保证数据的安全性和可用性,通常会采用分片(Sharding)和复制集(Replica Set)等技术。本文将详细介绍MongoDB的...

    阿里云MongoDB备份恢复功能说明和原理介绍.pdf

    这样的设计大大降低了操作的复杂度,使得技术人员可以更加便捷地进行数据备份和恢复。 总的来说,阿里云MongoDB的备份与恢复功能是在充分考虑分布式数据库特性的基础上设计的,它包含了多种备份方法,能够满足不同...

    MongoDB Java删除文档.pdf

    - 删除操作通常是不可逆的,所以在执行删除前,最好备份重要的数据。 - 使用`WriteConcern`来控制写操作的确认级别,确保数据的一致性。 综上所述,MongoDB的Java驱动提供了丰富的API,使得在Java应用中操作MongoDB...

    mongodb4.2全量定时增量备份python脚本

    MongoDB是一种流行的开源文档型数据库,它以高性能、高可用性和可扩展性著称。在生产环境中,定期备份是确保数据安全的重要环节。本话题主要围绕"mongodb4.2全量定时增量备份python脚本"展开,讲解如何利用Python...

    MongoDB实现备份压缩的方法教程

    MongoDB 是一个流行的开源文档型数据库,用于存储和管理非结构化或半结构化数据。在数据量庞大的情况下,数据库的备份和压缩至关重要,因为它们不仅有助于防止数据丢失,还能节省存储空间。本文将详细介绍如何在...

    mongodb备份数据库

    对于像MongoDB这样的NoSQL数据库,备份是防止数据丢失的关键步骤。 标题 "mongodb备份数据库" 指向的主题是关于如何对MongoDB数据库进行备份操作。MongoDB提供了多种备份方法,包括 mongodump 和 mongorestore ...

    mongodb入门时候文档

    - **文档(Document)**:MongoDB中的数据存储形式,类似于JSON对象,由键值对组成。 - **集合(Collection)**:类似关系数据库中的表,但不需预定义结构,文档可自由增删字段。 - **数据库(Database)**:存储...

    MongoDB 开发文档

    - 功能特性:支持Hadoop MapReduce任务读取MongoDB数据、支持Hadoop数据导入MongoDB等。 2. **Administrative Tools** - 包括但不限于:MongoDB-Based Applications、Admin UIs、HTTP Interface等。 - 这些工具...

    MongoDB安装文档

    MongoDB提供了丰富的命令行工具,如mongodump和mongorestore用于数据备份与恢复,以及mongotop和mongostat用于监控数据库状态。 总之,MongoDB的安装过程涉及多个步骤,不同的操作系统有不同的方法。通过阅读和遵循...

    MongoDB官方文档

    在MongoDB中,文档式数据模型是以JSON形式的文档来存储和组织数据,其设计强调灵活性和可扩展性。数据模型介绍部分会涉及到数据建模的概念、原则以及一些常见的数据建模案例和模式,数据模型参考部分则是对数据模型...

    mongodb备份数据和还原数据

    ### MongoDB数据备份 MongoDB提供了`mongodump`命令来创建数据库的备份。备份的基本语法如下: ```bash mongodump -h <hostname> -u <username> -p <password> -d <database_name> -c <collection_name> -o ``` ...

    mongoDB说明文档

    面向文档的数据存储是MongoDB的主要特征之一,这部分内容探讨了文档模型的优势和应用场景,以及如何在MongoDB中有效利用这一特性。 ### **40. “Connection Accepted”消息的大量出现(Whysomany...

    MongoDB官方实用文档

    - 探讨了MongoDB的设计理念和架构特点,包括文档型数据存储的优势、分片、复制集等关键概念。 #### 三、特定语言与工具支持 - **C# Language Center** - 介绍了如何使用C#与MongoDB进行交互,包括驱动程序的安装...

Global site tag (gtag.js) - Google Analytics