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

MongoDB uses mongoexport and mongoimport commands to bulk export and import JSON data to instances of the same table

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

The requirements are as follows: you need to modify all the data in a table in the database, so export them all, then modify them, and then import the modified data into mongo.

The details are as follows:

Backup, export a table as a json file

Specific commands:

Mongoexport-host localhost-port 27017-username ezsonaruser-password 123456-collection host_locations-db ezsonar_25-out / root/host_locations.json

Explanation:

-- localhost after host: 27017 after database ip--port to be exported: port number of instance node to be exported-- ezsonaruser after username: database user name-- 123456 after password: database user password-- widgets-test after collection: table name to be exported-- ezsonar_25 after db: database name to be exported-- / root/host_locations.json after out: file path to be exported (default is current folder)

After the export of the file, open the appearance is purple.

Then you can modify it in the editor. After the modification, save it. After saving it, you can import it into the database.

Import, bulk insert json data into a table

Specific commands:

Mongoimport-host localhost-port 27017-username ezsonaruser-password 123456-collection host_locations_test-db ezsonar_25-file / root/shaql/host_locations.json

Explanation:

-- localhost after host: 27017 after database ip--port to be imported: port number of instance node to be imported-- ezsonaruser after username: database user name-- 123456 after password: database user password-- host_locations_test after collection: table name to be imported-- ezsonar_25 after db: database name to be imported-- / root/shaql/host_locations.json after file: source text to be imported Item path (default is current folder)

Points to pay attention to:

1. Without entering the mongo command line, you can directly use the above two import and export commands.

2, import and export of json files, you do not want to wrap, the export is a line of data, you will modify the original to save directly, do not wrap. Instead of using a low editor like txt, change to a more advanced editor.

Finally, attach the help illustration of this command

The help information imported by mongoimport is shown as follows

The help information exported by mongoexport is shown as follows

In addition:

-f,-- fields: represents the fields in the collection, and you can select the exported fields according to the settings;-- type: represents the file type of the exported output, including csv and json files;-- skip: skips the specified amount of data;-- limit: reads the specified number of data records -- sort: sort the data, you can specify the sorted fields by parameters, and use 1 and-1 to specify the sorting method, where 1 is an ascending sort and-1 is used for descending sorting, such as sort ({KEY:1}). # Export type is json, database: mapdb, collection: bike field: bikeId,lat,lng,current_time,source, condition is that source field is ofo first data mongoexport-- port 27030-u sa-p Expressin@0618-d mapdb-c bike-f bikeId,lat,lng,current_time,source-- type=json-o bike.csv-- query=' {"source": "ofo"}'--limit=1# export type is csv Database: mapdb, collection: bike field: bikeId,lat,lng,current_time,source, if the source field is ofo first data mongoexport-- port 27030-u sa-p Expressin@0618-d mapdb-c bike-f bikeId,lat,lng,current_time,source-- type=csv-o bike.csv-- query=' {"source": "ofo"}'--limit=1

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

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

Database

Wechat

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

12
Report