MongoDB Backup recovery and export import

Source: Internet
Author: User
Tags mongodb server

Description: All operations in this article are in the MongoDB3.4.4 version under win7 .

First, backup and recovery

1. Backup:

Mongodump-h IP--port Port- u user name - p password - d database - o c9> file path

(a user account will be used to back up a database on a MONGODB server to a path)

If you do not need to specify the user, you can remove -u and - p, if youexport the local database, you can remove - h,if it is the default port, you can remove - Port;

if you want to export all databases, you can remove - D . If you only want to back up a collection, you should add the-C collection name option after the "-D database ."

Example of backing up a single collection:

If you want to back up data based on query criteria, you should add the '-Q query criteria ' option.

Example of a query condition backup:

(mongodb3.4.4 query condition using single quotation marks will error: query isnot inproper format)

2. Recovery:

Mongorestore-h IP--port Port- u user name - p password - d database C14>--drop file path

--drop: When recovering, delete the current database data, and then restore the backed up data (after recovery, the added after the backup, modified data will be deleted, use caution!)

If you do not use this option, the recovery process may have a number of key repeated error, the key duplication of data will not be restored)

If you only want to back up a collection, you should add the "-C collection name " option after the "-D Database " to back up a collection by specifying a specific Bson file instead of just specifying the file directory.

Example of recovering a single collection:

Second, Export and import

1. Export:

mongoexport-h IP--port port -u user name -p password -d database -c table name -f field name -q query condition --type=csv-o file path

Mongoexport You must use the- C parameter to specify the collection you want to export, so the- o parameter specifies a specific file name and cannot be a directory.

-F: Exports only the specified fields, separated by commas between multiple fields.

--type: Specifies the format of the exported file. Currently only JSON and CSVare supported, specifying that the CSV format must be exported using the- F The parameters specify the fields that need to be exported, andthe first behavior of theCSV file is the field name header.

2. Import:

(1) import json file:

Mongoimport-h IP--port Port- u user name - p password - d database - C Table name --upsert--drop file name

--upsert: The presence is updated, the insertion does not exist. To import only some of the fields, replace them with the --upsertfields parameter and specify a list of field names.

( 2 csv files:
mongoimport-h IP--port port -u user name -p password -d database -c table name --type=csv--headerline--upsert--drop filename

--headerline: Removes the header row from the CSV file, or specifies the field name using the -fields parameter, but the two parameters cannot be used at the same time.

MongoDB Backup recovery and export import

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.