preface

Recently, MongoDB export was used to export data, so here is a summary for future review.

To prepare data

use studentManagement; Db.score. Insert ({score: scores (80), scores: scores (80), scores: scores (80), scores: scores (80), scores: scores (80), scores: scores (80), scores: scores (80), scores: scores (80), scores: scores (80) ISODate("20210205")}, {score: 'Chinese ', score: NumberInt(100), exam_date: ISODate("20210205")}]}); Db.score. Insert ({id: scores (2), score: scores (90), score: scores (90), exam_date: ISODate("20210205")}, {score: 'Chinese ', score: NumberInt(100), exam_date: ISODate("20210205")}]});Copy the code

MongoExport

parameter

  • -h,–host: indicates the address of the remote database. By default, the local Mongo database is connected.
  • –port: indicates the port of the database that is connected remotely. The default remote port is 27017.
  • -u,–username: indicates the account used to connect to the remote database. If you want to set the database authentication, you need to specify the user account.
  • -p,–password: indicates the password of the account to connect to the database.
  • -d,–db: indicates the connected database.
  • -c,–collection: indicates the collection connected to the database.
  • -f, –fields: indicates the field in the collection. You can export the field based on the Settings.
  • –type: indicates the output file type, including CSV and JSON files.
  • -o, –out: indicates the name of the exported file.
  • -q, –query: indicates the query condition.
  • –skip: skip a specified amount of data;
  • –limit: reads a specified number of data records;
  • –sort: sort the data, you can specify the sort field by argument, and specify the sort method by using 1 and -1, where 1 is ascending and -1 is descending, as in sort({KEY:1}).

As you can see from the parameter type, there are two main export file formats: JSON and CSV.

JSON file format

Mongoexport -d studentManagement -c student -o student.json --authenticationDatabase admin -u username -p passwordCopy the code

After the execution, the data is displayed

You can see that there are two standard JSON formatted data in Student. json.

CSV file format

Mongoexport -d studentManagement -c student -o student. CSV --authenticationDatabase admin -u username -p password --type= CSV -f id,name,sex,scoresCopy the code

Note: Must be added in addition to adding –type= CSV-f List of required attributesOtherwise, the following error message is displayed

After the execution, the data is displayed

You see the first list of behavior attribute names, followed by two rows of data. Because scores is an array, it’s a string. Such string data does not conform to the required CSV format, and is not conducive to subsequent data analysis, so we do not want to see data. Therefore, it is inappropriate to simply use export here, so we need to process the data before exporting. The specific steps are as follows:

Before exporting the data as CSV, split the array data and save it to student_export
Db.student. Aggregate ([$unwind: "$scores"}, $unwind: "$scores"}, {id:"$id", name:"$name", sex:"$sex", course:"$scores.course", score: $scores. Score ", exam_date:"$scores. Exam_date ", _id: 0}}, // Save to student_export {$out: "student_export"},]);Copy the code

After executing, view the data:

At this point, the two pieces of data are split into four pieces.

Student_export is exported as a CSV file
Mongoexport -d studentManagement -c student_export -o student. CSV --authenticationDatabase admin -u username -p password --type= CSV  -f id,name,sex,course,score,exam_dateCopy the code

After the execution, the data is displayed

At this point, it meets the requirements of the CSV file format that we usually use to analyze data.

Finally, delete student_export

db.student_export.drop()
Copy the code