1. Export a collection
mongoexport -h 127.0.0.1:27017 -d DBNM -c collectionNM -o /tmp/collectionNM.bson
2. Import a collection
mongoimport -h 127.0.0.1:27017 -d DBNM -c collectionNM --upsert --drop /tmp/collectionNM.bson
--Import the specified fields from the file
mongoimport --db mydb --collection users --type csv --file ml-1m/users.dat --fields _id,gender,age,zip_code
Execution result |
connected to: 127.0.0.1 |
Mon Oct 9 22:10:11 imported 6040 objects |
About options |
--db Specify the DB to store. There is no need to create it in advance. |
#NAME? |
--type Specifies the file format to import. JSON, CSV and TSV can be used. CSV is specified here. |
--file Specify the file path to be imported. |
--fields Defines the fields of the data record to import. |
If you do not specify fields, an import error will occur. It should be clearly marked as a data delimiter. |
3. Backup the specified database to the tmp directory
mongodump -d test -o /tmp/
4. Restore the database
1.db.dropDatabase();
To restore the database from the specified directory, in general, you need to delete the current database first and then execute this command
2.mongorestore -d test /tmp/test
(Or use the --drop parameter to drop the database before restoring
mongorestore -d test --drop /tmp/test
)
3.还原某个collection
mongorestore -h 127.0.0.1:27017 -d DBNM --drop /tmp/collectionNM.bson