-------------------MongoDB Data Import and Export-------------------1, export tool: Mongoexport 1.1, Concept: MongoDB in the Mongoexport tool can put a Collec tion exported to a file in JSON or CSV format. You can specify the exported data item by parameter, or you can export the data based on the specified criteria. 1.2. Syntax:mongoexport-d dbname-c collectionname-o file--type json/csv-f fieldParameter Description:-d: Database name-c:collection name-o: Output filename--type: output format, default is Json-f : Output field, if-type is CSV, you need to add-f "field name" 1. 3, example: [[email protected] bin]#./mongoexport-d my_mongodb-c user-o User.dat
Connected to:127.0.0.1
Exported 2 records
[email protected] bin]# cat User.dat
{"_id": {"$oid": "4F81A4A1779282CA68FD8A5A"}, "UID": 2, "username": "Jerry", "Age": 100}
{"_id": {"$oid": "4f844d1847d25a9ce5f120c4"}, "UID": 1, "username": "Tom", "Age": 25}
[Email protected] bin]#. CVS [[email protected] bin]#./mongoexport-d my_mongodb-c User--csv-f Uid,username,a Ge-o
User_csv.dat
Connected to:127.0.0.1
Exported 2 records
[email protected] bin]# cat User_csv.dat
Uid,username,age
2, "Jerry", 100
1, "Tom", 25
[[Email protected] bin]# 2, data import: Mongoimport 2.1, Syntax:mongoimport-d dbname-c collectionname--file filename--headerline--type json/csv-f fieldParameter Description:-d: Database name-c:collection name--type: Imported format default json-f: Imported field name--hea Derline: If the imported format is CSV, you can use the header of the first row as the imported field--file: File to import 2.2, Example: 2.2.1 Import JSON data
Let's remove the table user first to demonstrate the effect
> Db.user.drop ();
True
> Show Collections;
System.indexes
>
Then import the data
[Email protected] bin]#/mongoimport-d my_mongodb-c user User.dat
Connected to:127.0.0.1
Imported 2 objects
[Email protected] bin]#
You can see that the table structure is created implicitly when importing data
2.2.2 Importing CSV data
Let's remove the table user first to demonstrate the effect
> Db.user.drop ();
True
> Show Collections;
System.indexes
>
Then import the data
[Email protected] bin]#./mongoimport-d my_mongodb-c user--type csv--headerline--file
User_csv.dat
Connected to:127.0.0.1
Imported 3 objects
[Email protected] bin]#
Parameter description:
-type indicates the file format to import
-headerline The first row is not imported because the first row is a column name
-file indicates the file path to import
Attention:
The CSV format is good and the mainstream database supports the export to CSV format, so this format is very beneficial for heterogeneous data migration - ------------------- MongoDB Backup and Recovery-------------------1, MongoDB database backup 1.1, syntax: Mongodump-h dbhost-d Dbname-o dbdirectory parameter description: -H:MONGDB server address, for example: 127.0 .0.1, of course you can also specify the port number:127.0.0.1:27017 -D: DB instance that needs to be backed up, such as:test & nbsp O: The data storage location of the backup, for example:/home/mongodump/, of course, the directory needs to be established in advance, this directory contains the database instance backup data. 1.2, instance: [[email protected] bin]#./mongodump-d my_mongodb-o my_mongodb_dump  ; 2, MongoDB Database recovery 2.1, Syntax: mongorestore-h dbhost-d dbname--dir dbdirectory &n Bsp parameter or name: -H:MONGODB server address & nbsp -D: The database instance that needs to be restored, for example: test, and of course the name can be different from the backup time, such as test2   --dir: Where to back up your data, such as:/home/mongodump/itcast/ --drop: When recovering, delete the current data first, Then restore the backed up data. That is, after the recovery, add the modified data after the backup will be deleted, use with caution! 2.2, example: [[email protected] bin]#./mongorestore-d My_mongodb my_mongodb_dump/* See also: http://www.cnblogs.com/qingtianyu2015/p/5968400.html http://blog.csdn.net/sxb0841901116/article/details/39894041
MongoDB Import Export and database backup