Database backup –mongodump
Back up all local MongoDB databases:
Copy Code code as follows:
# mongodump-h 127.0.0.1--port 27017-o/root/db/alldb
To back up a remote specified database:
Copy Code code as follows:
# mongodump-h 192.168.1.233--port 27018-d yourdb-o/root/db/yourdb
More Mongodump Detailed
Database Restore-mongorestore
Restore all databases to MongoDB:
Copy Code code as follows:
# mongorestore-h 127.0.0.1--port 27018/root/db/alldb
To restore the specified database:
Copy Code code as follows:
# Mongorestore--port 27017-d Yourdb/root/db/yourdb
More Mongorestore Detailed
Export Collection Data-mongoexport
To export data from a specified collection in a database:
Copy Code code as follows:
# mongoexport-h 192.168.1.233--port 27018-d yourdb-c Yourcoll-o/root/yourcoll.json
Exports the data from the specified field in the collection, and the exported file format is CSV:
Copy Code code as follows:
# mongoexport-d yourdb-c test-f "Id,name,score"--csv-o/root/test.csv
Export data based on criteria:
Copy Code code as follows:
# mongoexport-d Yourdb-c yourcoll-q ' {score:{$gt: '} '-o/root/yourcoll-bk.json
More Mongoexport Detailed
Collection Data Import-mongoimport
To restore the exported collection data:
Copy Code code as follows:
# mongoimport-d Yourdb-c Yourcoll--file/root/yourcoll.json
Import collection data, insert or update existing data:
Copy Code code as follows:
# mongoimport-d test-c yourcoll--file/root/yourcoll.json--upsert
More Mongoimport Detailed
MongoDB Database Cloning
Command format:
Copy Code code as follows:
Db.copydatabase (Fromdb, Todb, fromhost, username, password)
Copy the specified database from the remote MongoDB to the Local:
Copy Code code as follows:
# MONGO
> db.copydatabase ("Yii2", "Lyii2", "192.168.0.69")
More Db.copydatabase Detailed
Cloning of collections
Command format:
Copy Code code as follows:
Db.runcommand ({clonecollection: "<namespace>", From: "
Clones the specified collection from the remote MongoDB to the local database:
Copy Code code as follows:
# MONGO
> Db.runcommand ({clonecollection: "Test.user", From: "192.168.0.69", query:{}})
More Clonecollection Detailed