Mongo backup


Please indicate the source for reprint
Original typesetting addressClick for a better reading experience

Global backup and recovery


mongodump -d yea -o /data/db/3.1_2017_07_06_back
Migrate to native
scp -r [email protected]:/root/docker-data/mongo/3.1_2017_02_06_back /Users/zhidaliao/bak_dir/mongo


The docker mounting path is as follows:

    - /mnt/docker-data/testMongo3:/data/db

Copy the backup file to the folder / MNT / docker data / testmongo3, and enter / data / DB in docker

mongorestore -d yea 3.1_back/yea

Specifies the import and export of the collection

Export the data of the specified collection in the database:
 mongoexport -h --port 27018 -d yourdb -c yourcoll -o /root/yourcoll.json
Export the data of the specified field in the collection. The exported file format is CSV
mongoexport -d yourdb -c test -f "id,name,score" --csv -o /root/test.csv
Export data according to conditions:
mongoexport -d yourdb -c yourcoll -q '{score:{$gt:80}}' -o /root/yourcoll-bk.json
Restore exported collection data:
mongoimport -d yourdb -c yourcoll --file /root/yourcoll.json
Import collection data, insert or update existing data:
mongoimport -d test -c yourcoll --file /root/yourcoll.json --upsert

Mongodb database cloning

db.copyDatabase(fromdb, todb, fromhost, username, password)
Copy the specified database from remote mongodb to local:
 db.copyDatabase("yii2", "lyii2", "")
Clone of collection
db.runCommand({ cloneCollection: "<namespace>", from: "<hostname>", query: { <query> } });
Clone the specified collection from remote mongodb to the local database:
db.runCommand({  cloneCollection: "test.user", from: "", query:{}    })

Recommended Today

MySQL related

Indexes Bottom structure Disadvantages of hash table index: Using hash storage requires adding all files to memory, which consumes more memory space If all queries are equivalent queries, the hash is indeed fast, but in the actual scene, more data is found, and not all queries are equivalent queries, so the hash table is not […]