Get the App
SLTechnology News&Howtos  ›  Database  › 

Description of MongoDB backup and restore command

Shulou Source: shulou.com Published: 2022-06-01 16:11:46 10月03日 Update

I. full database backup and recovery

# mongodump-d my_mongodb-o my_mongodb_dump

# mongorestore-d my_mongodb my_mongodb_dump/*

II. JSON Collection backup and recovery

$mongoexport-d test-c book-o book.json

Connected to: 10.77.20.xx

Exported 2 records

$mongoimport-d test-c book book.json

Connected to: 10.77.20.xx

Imported 2 objects

3. CSV Collection data export and import

# more students.txt

{"classid": 1, "age": 20, "name": "kobe"}

{"classid": 1, "age": 23, "name": "nash"}

{"classid": 2, "age": 18, "name": "james"}

{"classid": 2, "age": 19, "name": "wade"}

{"classid": 2, "age": 19, "name": "bosh"}

{"classid": 2, "age": 25, "name": "allen"}

{"classid": 1, "age": 19, "name": "howard"}

{"classid": 1, "age": 22, "name": "paul"}

{"classid": 2, "age": 24, "name": "shane"}

#

1. Import data

# mongoimport-d test-c students students.txt

Connected to: 127.0.0.1

Tue Apr 22 16:02:32 imported 9 objects

> db.students.find () db.students.find ()

{"_ id": ObjectId ("53562218bc98e02b24984175"), "classid": 1, "age": 20, "name": "kobe"}

{"_ id": ObjectId ("53562218bc98e02b24984176"), "classid": 1, "age": 23, "name": "nash"}

{"_ id": ObjectId ("53562218bc98e02b24984177"), "classid": 2, "age": 18, "name": "james"}

{"_ id": ObjectId ("53562218bc98e02b24984178"), "classid": 2, "age": 19, "name": "wade"}

{"_ id": ObjectId ("53562218bc98e02b24984179"), "classid": 2, "age": 19, "name": "bosh"}

{"_ id": ObjectId ("53562218bc98e02b2498417a"), "classid": 2, "age": 25, "name": "allen"}

{"_ id": ObjectId ("53562218bc98e02b2498417b"), "classid": 1, "age": 19, "name": "howard"}

{"_ id": ObjectId ("53562218bc98e02b2498417c"), "classid": 1, "age": 22, "name": "paul"}

{"_ id": ObjectId ("53562218bc98e02b2498417d"), "classid": 2, "age": 24, "name": "shane"}

>

two。 Export CSV

# mongoexport-d test-c students-- csv-f classid,name,age-o students_csv.dat

Connected to: 127.0.0.1

Exported 9 records

#

Export only data, excluding _ id

Parameter description:

-h: indicates the IP of the database host

-u: indicates the user name of the database

-p: indicates the password of the database

-d: indicates the name of the database

-c: specify the name of collection

-f: indicates which columns to import

[root@jsmartserv hbk] # more students_csv.dat

Classid,name,age

1, "kobe", 20

1, "nash", 23

2, "james", 18

2, "wade", 19

2, "bosh", 19

2, "allen", 25

1, "howard", 19

1, "paul", 22

2, "shane", 24

[root@jsmartserv hbk] #

3. Delete data

Db.students.remove ()

4. Import CSV

# mongoimport-d test-c students-type csv-headerline-file students_csv.dat

Connected to: 127.0.0.1

Tue Apr 22 16:07:57 imported 9 objects

Parameter description:

-type: indicates the file format to import

-headerline: indicates that the first row is a column name and does not need to be imported

-file: indicates the file to import

5. Verification

Db.students.find () db.students.find ()

{"_ id": ObjectId ("5356235dbc98e02b2498417e"), "classid": 1, "name": "kobe", "age": 20}

{"_ id": ObjectId ("5356235dbc98e02b2498417f"), "classid": 1, "name": "nash", "age": 23}

{"_ id": ObjectId ("5356235dbc98e02b24984180"), "classid": 2, "name": "james", "age": 18}

{"_ id": ObjectId ("5356235dbc98e02b24984181"), "classid": 2, "name": "wade", "age": 19}

{"_ id": ObjectId ("5356235dbc98e02b24984182"), "classid": 2, "name": "bosh", "age": 19}

{"_ id": ObjectId ("5356235dbc98e02b24984183"), "classid": 2, "name": "allen", "age": 25}

{"_ id": ObjectId ("5356235dbc98e02b24984184"), "classid": 1, "name": "howard", "age": 19}

{"_ id": ObjectId ("5356235dbc98e02b24984185"), "classid": 1, "name": "paul", "age": 22}

{"_ id": ObjectId ("5356235dbc98e02b24984186"), "classid": 2, "name": "shane", "age": 24}

>

Query the students collection to verify that the import was successful:

Tags: Data database backup parameters name file authentication success one line only host password format user user name query command Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Huawei Microsoft Docker