"MongoDB Getting Started Tutorial" Chapter 31 Data Import and Export

This article will introduce how to use the mongoimport tool to import files into the local MongoDB database server, and how to use the mongoexport tool to export data in MongoDB to files.

MongoDB Database Tools

MongoDB provides a range of database tools , including:

  • Binary import/export tools mongodump, mongorestore, and bsondump;
  • Data import/export tools mongoimport and mongoexport;
  • Diagnostic tools mongostat and mongotop;
  • GridFS tools mongofiles.

Starting with MongoDB 4.4, these database tools are no longer distributed and installed with the server, but use separate packages.

First, select the corresponding version, platform, and installation file format through the download page .

insert image description here
Then run the installation file and follow the prompts to install it.

At the same time, we also need to click to download the sample file movies.json .

Import files using mongoimport

The mongoimport tool can import JSON, CSV and TSV files into the MongoDB server.

First, enter the installation directory on the command line. The Windows platform defaults to C:\Program Files\MongoDB\Tools\100\bin.

cd "C:\Program Files\MongoDB\Tools\100\bin"

Then, execute the mongoimport command to import the movies.json file into the MongoDB server:

mongoimport.exe D:\data\movies.json -d bookdb -c movies --drop

Among them, -d is used to specify the target database, -c is used to specify the target collection, and – drop means that if the collection already exists, delete it and then import it.

After the import is successful, the following information will be displayed:

2023-03-30T11:49:43.954+0800    connected to: mongodb://localhost/
2023-03-30T11:49:44.028+0800    dropping: bookdb.movies
2023-03-30T11:49:45.298+0800    3201 document(s) imported successfully. 0 document(s) failed to import.

Then connect to the MongoDB server and query the imported movies collection:

db.movies.countDocuments()
3201

db.movies.findOne()
{
    
    
  _id: ObjectId("642506d80e4683c5c14d7fa8"),
  Title: "Let's Talk About Sex",
  'US Gross': 373615,
  'Worldwide Gross': 373615,
  'US DVD Sales': null,
  'Production Budget': 300000,
  'Release Date': 'Sep 11 1998',
  'MPAA Rating': null,
  'Running Time min': null,
  Distributor: 'Fine Line',
  Source: null,
  'Major Genre': 'Comedy',
  'Creative Type': null,
  Director: null,
  'Rotten Tomatoes Rating': 13,
  'IMDB Rating': null,
  'IMDB Votes': null
}

Export files using mongoexport

The mongoexport tool can export the contents of the MongoDB database as JSON or CSV files.

Export the data in the MongoDB instance

The following command is used to export the collection movies data in the local MongoDB database bookdb on port 27017 to the movies.json file:

mongoexport.exe --collection=movies --db=bookdb --out=movies.json

Among them, –collection is used to specify the collection to be exported, –db specifies the database where the collection is located, and –out is used to specify the exported file path and name.

If you want to export data from a remote MongoDB instance, you need to specify the --uri connection string, for example:

mongoexport.exe --uri="mongodb://mongodb0.remote.server:27017/bookdb"  --collection=movies --out=movies.json

In addition, we can also specify the server address and port through the --host and --port parameters. For example:

mongoexport.exe --host="mongodb0.remote.server" --port=27017 --collection=movies --db=bookdb --out=movies.json

Export the data in the replica set

If you want to export the data in the replica set, you can specify the replica set and members in the --uri connection string:

mongoexport.exe --uri="mongodb://mongodb0.remote.server:27017,mongodb1.remote.server:27017,mongodb2.remote.server:27017/bookdb?replicaSet=myReplicaSetName" --collection=movies --out=movies.json

Alternatively, the replica set and members can also be specified in the --host parameter:

mongoexport.exe --host="myReplicaSetName/mongodb0.remote.server:27017,mongodb1.remote.server:27017,mongodb2.remote.server:27017" --collection=movies --db=bookdb --out=movies.json

By default, mongoexport reads data through the primary node of the replica set. However, we can modify this configuration by specifying the read priority. For example:

mongoexport.exe --uri="mongodb://mongodb0.remote.server:27017,mongodb1.remote.server:27017,mongodb2.remote.server:27017/bookdb?replicaSet=myReplicaSetName&readPreference=secondary" --collection=movies --out=movies.json

The above command will read data from the slave nodes of the replica set.

Alternatively, you can also specify the read node through the --readPreference parameter:

mongoexport.exe --host="myReplicaSetName/mongodb0.remote.server:27017,mongodb1.remote.server:27017,mongodb2.remote.server:27017" --readPreference=secondary --collection=movies --out=movies.json

Export the data in the sharded cluster

If you want to export the data in the shard cluster, you can specify the address of the mongos instance in the --uri connection string. For example:

mongoexport.exe --uri="mongodb://mongos0.remote.server:27017/bookdb" --collection=movies --out=movies.json

Or you can specify the address and port of the mongos instance in the --host parameter:

mongoexport.exe --host="mongos0.remote.server:27017" --collection=movies --db=bookdb --out=movies.json

Guess you like

Origin blog.csdn.net/horses/article/details/129202158