Introduction to mongoexport and mongoimport of mongodb

Environment: mongodb3.6.16 binary installation

1. Introduction to mongoexport parameters and syntax:

The mongoexport tool in Mongodb can export a collection to a JSON or CSV file.Exported data items can be specified by parameters, or data can be exported according to specified conditions.

Mongoexport-h IP--port port port-u username-p password-d database-c table name-f field-q condition export--csv-o file name

-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, separated by commas, -f uid,name,age exports uid,name,age three fields
-o Specifies the name of the file to be exported
-q specifies the filter condition for the exported data, -q'{'uid':'100'}'exports data with uid 100
--type Specifies the file type
--Name of authenticationDatabase validation data

Export entire table data:

[root@localhost ~]# mongoexport -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin -d dbtest002 -c mumu  -o ./1.dat
2020-01-05T11:54:48.956+0800    connected to: 127.0.0.1:6068
2020-01-05T11:54:48.956+0800    exported 3 records

[root@localhost ~]# cat 1.dat 
{"_id":{"$oid":"5e0f162d1083b09e85237cb4"},"name":"Floret","grade":"second grade","Gender":"male","hobby":"Study"}
{"_id":{"$oid":"5e0f161d1083b09e85237cb3"},"name":"Floret","grade":"second grade","Gender":"male","hobby":"Study"}
{"_id":{"$oid":"5e0f16191083b09e85237cb2"},"name":"Floret","grade":"second grade","Gender":"male","hobby":"Study"}

Export data for the specified field of the table:

[root@localhost ~]# Mongoexport-h 127.0.0.1:6068-u root-p TdLLQ6689 --authenticationDatabase admin-d dbtest002-c mumu-f name,'grade','gender'-o. /2.dat
2020-01-05T11:55:41.187+0800    connected to: 127.0.0.1:6068
2020-01-05T11:55:41.187+0800    exported 3 records
[root@localhost ~]# cat 2.dat 
{"_id":{"$oid":"5e0f162d1083b09e85237cb4"},"name":"Floret","grade":"second grade","Gender":"male"}
{"_id":{"$oid":"5e0f161d1083b09e85237cb3"},"name":"Floret","grade":"second grade","Gender":"male"}
{"_id":{"$oid":"5e0f16191083b09e85237cb2"},"name":"Floret","grade":"second grade","Gender":"male"}

Export data in csv format for the specified fields of the table:

[root@localhost ~]# mongoexport-h 127.0.0.1:6068-u root-p TdLLQ6689 --authenticationDatabase admin-d dbtest002-c mu --type csv-f name,'grade'-o. /3.csv
[root@localhost ~]# cat 3.csv 
name, grade
 Flowers, 2nd grade
 Flowers, 2nd grade
 Flowers, 2nd grade
[root@localhost ~]# mongoexport-h 127.0.0.1:6068-u root-p TdLLQ6689 --authenticationDatabase admin-d dbtest002-c Mumu --csv-f name,'grade'-o. /1.csv
2020-01-05T11:58:56.598+0800    csv flag is deprecated; please use --type=csv instead
2020-01-05T11:58:56.599+0800    connected to: 127.0.0.1:6068
2020-01-05T11:58:56.600+0800    exported 3 records
[root@localhost ~]# cat 1.csv 
name, grade
 Flowers, 2nd grade
 Flowers, 2nd grade
 Flowers, 2nd grade

Export a json format file. The default export is a json format data file:

[root@localhost ~]# Mongoexport-h 127.0.0.1:6068-u root-p TdLLQ6689 --authenticationDatabase admin-d dbtest002-c Mumu --type json-f name,'grade'-o. /2.json
2020-01-05T12:01:28.951+0800    connected to: 127.0.0.1:6068
2020-01-05T12:01:28.951+0800    exported 3 records
[root@localhost ~]# cat 2.json 
{"_id":{"$oid":"5e0f162d1083b09e85237cb4"},"name":"Floret","grade":"second grade"}
{"_id":{"$oid":"5e0f161d1083b09e85237cb3"},"name":"Floret","grade":"second grade"}
{"_id":{"$oid":"5e0f16191083b09e85237cb2"},"name":"Floret","grade":"second grade"}

Export data according to conditions:
Number of hobby records:

[root@localhost ~]# Mongoexport-h 127.0.0.1:6068-u root-p TdLLQ6689 --authenticationDatabase admin-d dbtest002-c chenji-q'{hobbies"}'-o. /daqiu.json
2020-01-05T12:10:21.788+0800    connected to: 127.0.0.1:6068
2020-01-05T12:10:21.788+0800    exported 2 records
[root@localhost ~]# cat daqiu.json 
{"_id":{"$oid":"5e1160a3ef45ab936b74982a"},"name":"Li Si","grade":"first grade","Gender":"female","hobby":"Play a ball"}
{"_id":{"$oid":"5e116102ef45ab936b74982d"},"name":"Zhao Wu","grade":"fifth grade","Gender":"male","hobby":"Play a ball"}

2. Introduction to the syntax and parameters of mongoimport:

The mongoimport tool in Mongodb imports the contents of a specific format file into a specified collection.This tool can import data in either JSON format or CSV format.

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 Specifies the name of the file to be exported
-q specifies the filter conditions for the exported data
--Delete the original before drop ping
--headerline indicates that the first row is the column name and does not need to be imported.
-j Number of insert operations running simultaneously (default 1), parallel
--Name of authenticationDatabase validation data

Export the data and restore it to the table:
Export data:

[root@localhost ~]# mongoexport -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin -d dbtest002 -c fenshu   -o ./111.bat
2020-01-05T12:46:21.264+0800    connected to: 127.0.0.1:6068
2020-01-05T12:46:21.265+0800    exported 8 records

Restore to table:

[root@localhost ~]# mongoimport -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin -d dbtest002 -c fenshu --drop ./111.bat
2020-01-05T12:50:07.154+0800    connected to: 127.0.0.1:6068
2020-01-05T12:50:07.154+0800    dropping: dbtest002.fenshu
2020-01-05T12:50:07.181+0800    imported 7 documents

Table data import for some fields:
Specify field export:

[root@localhost ~]# Mongoexport-h 127.0.0.1:6068-u root-p TdLLQ6689 --authenticationDatabase admin-d dbtest002-c fenshu--type json-f name,'grade'-o. /222.bat
2020-01-05T12:57:16.030+0800    connected to: 127.0.0.1:6068
2020-01-05T12:57:16.030+0800    exported 7 records

Specify fields to import tables without primary key conflicts, so add a parameter--drop

[root@localhost ~]# Mongoimport-h 127.0.0.1:6068-u root-p TdLLQ6689--authenticationDatabase admin-d dbtest002-c fenshu--upsertFields name,'grade'--drop. /222.bat 
2020-01-05T12:58:22.422+0800    connected to: 127.0.0.1:6068
2020-01-05T12:58:22.428+0800    imported 7 documents

Tags: Linux JSON Database MongoDB

Posted on Sun, 05 Jan 2020 11:27:37 -0800 by matthewc