Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use mongoexport and mongoimpo in mongodb

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to use mongoexport and mongoimpo in mongodb? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

1. Introduction to mongoexport parameters and syntax:

The mongoexport tool in Mongodb can export an collection to a file in JSON or CSV format. You can specify exported data items through parameters, or you can export data based on specified criteria.

Mongoexport-h IP-- port port-u user name-p password-d database-c table name-f field-Q conditional 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 of the database

-d indicates the name of the database

-c specify the name of collection

-f indicates which columns to export, separated by commas, and-f uid,name,age exports the uid,name,age fields

-o indicate the name of the file to be exported

-Q indicates the filter condition for exported data, and-Q'{"uid": "100"} 'exports data with a uid of 100

-- type specifies the file type

-- name of authenticationDatabase authentication data

Export the entire table data:

[root@localhost] # mongoexport-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c mumu-o. / 1.dat2020-01-05T11:54:48.956+0800 connected to: 127.0.0.1 root 6068 2020-01-05T11:54:48.956+0800 exported 3 records [root@localhost ~] # cat 1.dat {"_ id": {"$oid": "5e0f162d1083b09e85237cb4"}, "name": "floret", "Grade": "sophomore" "gender": "male", "hobby": "learning" {"_ id": {"$oid": "5e0f161d1083b09e85237cb3"}, "name": "floret", "grade": "second grade", "gender": "male", "hobby": "learning" {"_ id": {"$oid": "5e0f16191083b09e85237cb2"}, "name": "floret", "grade": "second grade", "gender": "male", "hobby": "learning"}

Export the data for the fields specified in the table:

[root@localhost ~] # mongoexport-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c mumu-f name,' Grade', 'gender'-o. / 2.dat2020-01-05T11:55:41.187+0800 connected to: 127.0.0.1 TdLLQ6689 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 the csv format data for the fields specified in the table:

[root@localhost] # mongoexport-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c mumu-- type csv-f name,' Grade'- o. / 3.csv [root@localhost ~] # cat 3.csv name, Grade 2 [root@localhost] # mongoexport-h 127.0.1 Fran 6068-u root-p TdLLQ6689-authenticationDatabase admin-d dbtest002-c mumu-- csv-f name Grade-o. / 1.csv2020-01-05T11:58:56.598+0800 csv flag is deprecated Please use-- type=csv instead2020-01-05T11:58:56.599+0800 connected to: 127.0.0.1 05T11:58:56.599+0800 connected to 6068 2020-01-05T11:58:56.600+0800 exported 3 records [root@localhost ~] # cat 1.csv name, Grade floret, Grade 2

Export a file in json format. By default, the data file in json format is exported:

[root@localhost ~] # mongoexport-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c mumu-- type json-f name,' Grade'- o. / 2.json2020-01-05T12:01:28.951+0800 connected to: 127.0.0.1 root 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 based on conditions:

Number of records that like to play ball:

[root@localhost ~] # mongoexport-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c chenji-Q'{"hobby": "playing"}'- o. / daqiu.json2020-01-05T12:10:21.788+0800 connected to: 127.0.0.1 Visa 60682020-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": "playing ball"} {"_ id": {"$oid": "5e116102ef45ab936b74982d"}, "name": "Zhao Wu", "grade": "fifth grade", "gender": "male", "hobby": "playing"} II, introduction to mongoimport grammar and parameters:

The mongoimport tool in Mongodb can import the contents of a specific format file into a specified collection. The 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 of the database

-d indicates the name of the database

-c specify the name of collection

-f indicates which columns to export

-o indicate the name of the file to be exported

-Q indicates the filtering criteria for the exported data

Delete the original drop before inserting it

Headerline indicates that the first row is the column name and does not need to be imported.

-j insert operations running at the same time (default is 1), parallel

-- name of authenticationDatabase authentication data

Export the data, and then restore to the table:

Export data:

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

Restore to the table:

[root@localhost] # mongoimport-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c fenshu-- drop. / 111.bat2020-01-05T12:50:07.154+0800 connected to: 127.0.0.1 05T12:50:07.154+0800 dropping 60682020-01-05T12:50:07.154+0800 dropping: dbtest002.fenshu2020-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 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c fenshu-- type json-f name,' Grade'- o. / 222.bat2020-01-05T12:57:16.030+0800 connected to: 127.0.0.1 Vera 6068 2020-01-05T12:57:16.030+0800 exported 7 records

Specifies that fields are imported into the table to avoid primary key conflicts, so add the parameter-- drop

[root@localhost] # mongoimport-h 127.0.0.1 TdLLQ6689 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.1 root 6068 2020-01-05T12:58:22.428+0800 imported 7 documents's questions on how to use mongoexport and mongoimpo in mongodb are shared here I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report