MongoDB study notes (4)-file import/export

To import and export data in MongoDB, you can use mongoexport.exe and mongoimport.exe in the bin directory of the installation directory 

During operation, you don't need to log in to MongoDB, just operate directly in the cmd command line . First, you need to enter the bin folder in the MongoDB installation directory.

cd C:\Program Files\MongoDB\Server\4.0\bin

For convenience, you can add this directory to the system's environment variables , so that you don't need to load the address every time you start cmd, and it can be used directly.


 File import (mongoimport)

Import command:

mongoimport -h dbhost -d dbname -c collectionname file

  • dbhost: database address, the IP and port where the MongoDB server is located, such as localhost:27017
  • dbname: specify the library used, specify the database instance used, such as test
  • collectionname: Specify the collection to be imported, such as c1, c2, and it can be inconsistent with the export. You can customize it. If it does not exist, it will be created directly
  • file: the mongoDB collection file exported in advance

Example:

mongoimport -h localhost:27017 -d test -c pyaa_test F:/c1.csv

Batch import of multiple files:

cd "C:\Users\Desktop\40079-wyc\audio"

FOR %i IN (*_ST2.csv) DO "C:\Program Files\MongoDB\Server\4.0\bin\mongoimport" --db test --collection pyaa --type csv --headerline --ignoreBlanks --file %i --host localhost:27017 --authenticationDatabase test -u "root" -p "123456"


File export (mongoexport)

Export command:

mongoexport -h dbhost -d dbname -c collectionname -o output

  • dbhost: database address, the IP and port where the MongoDB server is located, such as localhost:27017
  • dbname: Specify the database instance used, such as test
  • collectionname: specify the collection to be exported, such as c1
  • output: Specify the name of the file to be exported, such as F:/c1.csv. Note that it is a file, not a directory. If the directory does not exist, it will be created together

Export files can have many forms, such as json, csv, txt, xls, docs, etc.

Example:

mongoexport -h localhost:27017 -d test -c c1 -o F:/c1.csv

Guess you like

Origin blog.csdn.net/qq_14997473/article/details/89639072