, two tools are located in the bin directory of the MongoDB installation directory
Here's how they are used:
One
Export Tool Mongoexport
The Mongoexport tool in MongoDB can export a collection 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.
Use the mongoexport--help command to see how it's done
Parameter description:
- -H: Indicates the IP of the database host
- -U: Indicates the user name of the database
- -P: Indicates the password for the database
- -D: Indicates the name of the database
- -C: Indicates the name of the collection
- -F: Indicates which columns to export
- -O: Indicates the file name to be exported
- -Q: Indicates the filtering criteria for the exported data
Example:
Export data from the students collection under the goods database
Execute the commands in the diagram
The Students.dat file is generated under the Bin directory with the following contents (you can also perform type students.dat viewing on the command line)
As can be seen from the above results, we did not display the specified export style when exporting the data, and the data in JSON format was exported by default. In fact, it is often necessary to export data in CSV format, the command is as follows
mongoexport-d goods-c students--csv-f classid,name,age-o Students_csv.dat
Detailed parameters:
- -D: Indicates which library to use, in this case goods
- -C: Indicates the collection to be exported, in this case students
- -O: Indicates the name of the file to be exported, in this case Students_csv.dat
- -csv: Indicates that you want to export to CSV format
- -F: Indicates that data for the 3 columns of ClassID, name, and age need to be exported
View Files
Two
Import Tool Mongoimport
The Mongoimport tool in MongoDB can import content from a particular format file into the specified collection. The tool can import JSON-formatted data or import CSV format data.
Use the mongoimport--help command to see how it's done
Parameter description:
- -H: Indicates the IP of the database host
- -U: Indicates the user name of the database
- -P: Indicates the password for the database
- -D: Indicates the name of the database
- -C: Indicates the name of the collection
- -F: Indicates which columns to import
Example
Delete Students collection data first, verify
Db.students. remove ({});d b.students.find ();
Import a Students.dat file that was previously exported
The above shows the contents of the file imported in JSON format, if you want to import the contents of the CSV format file, you need to specify the import format through the--type parameter
mongoimport-d goods-c students--type csv--headerline--file students_csv.dat
Detailed parameters
- -D: Indicates the database name, in this case goods
- -C: Specify collection name, in this case students
- -type: Indicates the file format to import
- -headerline: Indicates that the first row is a column name and does not need to be imported
- -file: Indicates the file to import
- Students_csv.dat: Imported file name
Tools can also be used for import and export, such as: NoSQL Manager for MongoDB provides import and export in many formats
MongoDB Data Export tool mongoexport and Import tool Mongoimport use