MongoDB Export Import Backup recovery data detailed explanation and example _mongodb

Source: Internet
Author: User
Tags mongodb first row mongodump mongorestore

The importance of database backup and data recovery, I think you know, here is an example of how to manipulate data backups, examples of data recovery:

Creating test data

Create Db:testdb,collection:user, insert 10 records

MONGO MongoDB Shell version:3.0.2 connecting to:test > Use TestDB switched to DB TestDB > Db.user.insert ({id:1,na
Me: "User 1"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:2,name: "User 2"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:3,name: "User 3"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:4,name: "User 4"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:5,name: "User 5"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:6,name: "User 6"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:7,name: "User 7"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:8,name: "User 8"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:9,name: "User 9"});
Writeresult ({"ninserted": 1}) > Db.user.insert ({id:10,name: "User 10"});
Writeresult ({"ninserted": 1}) > > Db.user.find ();
{"_id": ObjectId ("574d7aae8780832e6c4e27b4"), "id": 1, "name": "User 1"} {"_id": ObjectId ("574d7aaf8780832e6c4e27b5"), "id": 2, "name": "User 2" } {"_id": ObjectId ("574d7aaf8780832e6c4e27b6"), "id": 3, "name": "User 3"} {"_id": ObjectId ("574d7aaf8780832e6c4e27b7 ")," id ": 4," name ":" User 4 "} {" _id ": ObjectId (" 574d7aaf8780832e6c4e27b8 ")," id ": 5," name ":" User 5 "} {" _id ": Object ID ("574D7AAF8780832E6C4E27B9"), "id": 6, "name": "User 6"} {"_id": ObjectId ("574d7aaf8780832e6c4e27ba"), "id": 7, "name ": User 7} {" _id ": ObjectId (" 574D7AAF8780832E6C4E27BB ")," id ": 8," name ":" User 8 "} {" _id ": ObjectId (" 574d7aaf8780832
 E6C4E27BC ")," id ": 9," name ":" User 9 "} {" _id ": ObjectId (" 574D7AB08780832E6C4E27BD ")," id ": Ten," Name ":" User 10 "}

Data export Mongoexport

Parameter description:

-D database name
-C Collection Name
File name of-O output
--type output format, default to JSON
-F Output field, if--type is CSV, you need to add-f "field name"

More parameter descriptions can refer to Mongoexport–help

Example: Export user all records to/tmp/user.json

mongoexport-d testdb-c user-o/tmp/user.json
2016-05-31t20:00:32.257+0800 connected
2016-05-31t20:00:32.286+0800  exported Records

Cat/tmp/user.json
{"_id": {"$oid": " 574d7aae8780832e6c4e27b4 "}," id ": 1," name ":" User 1 "}
{" _id ": {" $oid ":" 574d7aaf8780832e6c4e27b5 "}," id ": 2," name ":" User 2 "}
{" _id ": {" $oid ":" 574d7aaf8780832e6c4e27b6 "}," id ": 3," name ":" User 3 "}
{" _id ": {" $oid ":" 574d7aaf8780832e6c4e27b7 "}," id ": 4," name ":" User 4 "}
{" _id ": {" $oid ":" 574d7aaf8780832e6c4e27b8 "}," id ": 5," name ": "User 5"}
{"_id": {"$oid": "574d7aaf8780832e6c4e27b9"}, "id": 6, "name": "User 6"}
{"_id": {"$oid": " 574d7aaf8780832e6c4e27ba "}," id ": 7," name ":" User 7 "}
{" _id ": {" $oid ":" 574D7AAF8780832E6C4E27BB "}," id ": 8," name ": "User 8"}
{"_id": {"$oid": "574D7AAF8780832E6C4E27BC"}, "id": 9, "name": "User 9"}
{"_id": {"$oid": " 574D7AB08780832E6C4E27BD "}," id ": Ten," Name ":" User 10 "}

Example: Export user all IDs to/tmp/user.csv

The format is CSV but no fields can be faulted

mongoexport-d testdb-c user--type csv-o/tmp/user.csv 2016-05-31t20:01:05.393+0800 failed:csv  mode requires a Field List

mongoexport-d testdb-c user--type csv-f "id"-o/tmp/user.csv  con nected to:localhost
2016-05-31t20:01:46.534+0800  exported records

cat/tmp/user.csv
ID
1
2
3
4
5
6
7 8 9 10

Data import Mongoimport

Parameter description:

-D database name
-C Collection Name
--type imported format, default JSON
-F Imported Field names
--headerline if the imported format is CSV, you can use the header of the first row as the imported field
--file Files to import

More parameter descriptions can refer to Mongoimport–help

Empty collection user before importing

> Db.user.drop ();
True
> Db.user.find ();
> 

Example: Import the User.json from the example above

mongoimport-d testdb-c user--file/tmp/user.json
2016-05-31t20:10:22.240+0800  connected To:localhost
2016-05-31t20:10:22.287+0800  Imported Documents

Example: Import the user.csv from the example above

mongoimport-d testdb-c user--type csv--headerline--file/tmp/user.csv
2016-05-31t20:11:28.975+0800  Connected to:localhost
2016-05-31t20:11:29.003+0800  Imported Documents

Data backup Mongodump

Parameter description:

-D database name
-C Collection Name
File path for-o backup

More parameter descriptions can refer to Mongodump–help

Example: Back up the TestDB user to/tmp

mongodump-d testdb-c user-o/tmp
2016-05-31t20:18:25.813+0800 writing Testdb.user to/tmp/testdb/user.bson<  c3/>2016-05-31t20:18:25.818+0800  Writing Testdb.user metadata To/tmp/testdb/user.metadata.json
2016-05-31t20:18:25.849+0800 Done  Dumping Testdb.user

Data Recovery Mongorestore

Parameter description:

-D database name
-C Collection Name

More parameter descriptions can refer to Mongorestore–help

Empty collection user before importing

> Db.user.drop ();
True
> Db.user.find ();
>

Example: Restore the data from the previous example backup

mongorestore-d testdb-c User/tmp/testdb/user.bson 
2016-05-31t20:21:23.050+0800  checking for collection data In/tmp/testdb/user.bson
2016-05-31t20:21:23.084+0800  reading metadata file from/tmp/testdb/ User.metadata.json
2016-05-31t20:21:23.088+0800  restoring testdb.user from File/tmp/testdb/user.bson
2016-05-31t20:21:23.153+0800  restoring indexes for collection Testdb.user from metadata
2016-05-31t20 : 21:23.156+0800  finished restoring testdb.user
2016-05-31t20:21:23.156+0800  Done

Thank you for reading, I hope to help you, thank you for your support for this site!

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.