Mongodump backing up the database
Common Backup command formats
Mongodump-h IP--port Port-u user name-p password-d database-o file exists path
If you want to export all databases, you can remove the-D
Mongodump Syntax:
[Email protected] ~]# Mongodump--help
Export MongoDB data to BSON files.
Options
--HELP Produce Help message
-V [--verbose] is more verbose (include multiple times
For more verbosity e.g.-VVVVV)
--version Print the program ' s version and exit
-h [--host] arg MONGO host to connect to (<set
NAME>/S1,S2 for sets)
--port ARG server port. Can also use--host
Hostname:port
--ipv6 Enable IPV6 support (disabled by
Default
-U [--username] arg username
-P [--password] arg password
--authenticationdatabase arg user source (defaults to dbname)
--authenticationmechanism Arg (=MONGODB-CR)
Authentication mechanism
--dbpath arg directly access Mongod database files
In the given path, instead of
Connecting to a Mongod server-needs
To-Lock the data directory, so cannot
be used if a mongod is currently
Accessing the same path
--directoryperdb each db are in a separate directly
(relevant only if dbpath specified)
--journal enable journaling (relevant only if
DBPath specified)
-d [--DB] arg database to use
-c [--collection] arg collection to use (some commands)
-O [--out] arg (=dump) output directory or "-" for stdout
-Q [--query] arg JSON query
--oplog use Oplog for Point-in-time
Snapshotting
--repair try to recover a crashed database
--forcetablescan Force a table scan
$snapshot)
Export Database
[Email protected] ~]# mongodump-h 127.0.0.1--port 30216-d test-uxxxx-pxxxxx-o home/mongodb/
Connected to:10.10.3.245:30216
Thu 02:15:04.529 database:test To/home/mongodb/test
Mongorestore Restoring a Database
Common Command Formats
Mongorestore-h IP--port Port-u user name-p password-d database--drop file exists path
[Email protected] mongodb]# mongorestore-d test/home/mongodb/test #test这个数据库的备份路径
These two commands can be implemented to backup and restore the database, the file format is JSON and Bson
mongoexport Export a table, or some field in a table
Common Command Formats
Mongoexport-h IP--port Port-u user name-p password-D database-C table name-F field
-Q Condition Export--csv-o file name above parameters good understanding, focus on:
-F export refers to field, split by font size,-f name,email,age export name,email,age three fields
-Q can export the root query condition,-Q ' {"_id": "10001"} ' exports the data with UID 100
--csv means that the exported file format is CSV, which is useful because most of the relational databases are supported by CSV, and there's something in common here.
export an entire sheet
[Email protected] mongodb]# mongoexport-d test-c users-o/home/mongodb/test/users.dat
Connected to:127.0.0.1
Exported Records
export some fields in a table
[Email protected] mongodb]# mongoexport-d test-c users--csv-f uid,name,sex-o test/users.csv
Connected to:127.0.0.1
Exported Records
dare to out data according to conditions
[[email protected] mongodb]# mongoexport-d test-c users-q ' {uid:{$gt: 1}} '-O Test/users.json
Connected to:127.0.0.1
Exported Records
mongoimport Import a table, or some field in a table
restore a non-CSV file exported from an entire table
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsert--drop file name
Focus on--upsert, the other arguments above have been mentioned,--upsert insert or update existing data
Restore an export file for a partial field
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsertfields field--drop file name
--upsertfields Root--upsert like
Restore the exported CSV file
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--type type--headerline--upsert--drop file name
In the above three cases, there can be other permutations and combinations.
To restore exported table data
[Email protected] mongodb]# mongoimport-d test-c users--upsert Test/users.dat
Connected to:127.0.0.1
............
Table data import for partial fields
[Email protected] mongodb]# mongoimport-d test-c users--upsertfields uid,name,sex test/users.dat
Connected to:127.0.0.1
...............................................
Restore a CSV file
[Email protected] mongodb]# mongoimport-d test-c users--type csv--headerline--file test/users.csv
Connected to:127.0.0.1
...........................................
MongoDB Backup, restore, import, export