📅  最后修改于: 2023-12-03 15:17:41.625000             🧑  作者: Mango
When working with MongoDB, it's often necessary to import data from external sources such as CSV files, JSON files, or another database system. This is where the MongoDB import tool comes in handy. The import tool allows you to easily and quickly import data into your MongoDB database.
The general syntax of the MongoDB import command is as follows:
mongoimport [options] [file]
Here, options
refers to any additional parameters, and file
refers to the file from which you are importing data.
There are several options that you can use with the MongoDB import tool to customize the import process to suit your needs. Here are some of the most commonly used options:
To import data from a CSV file, enter the following command:
mongoimport --db mydatabase --collection mycollection --type csv --file myfile.csv --headerline
This command imports the data from the myfile.csv
file into the mycollection
collection in the mydatabase
database. The --headerline
option tells MongoDB to treat the first row in the CSV file as a header row.
To import data from a JSON file, enter the following command:
mongoimport --db mydatabase --collection mycollection --type json --file myfile.json
This command imports the data from the myfile.json
file into the mycollection
collection in the mydatabase
database.
To import data and update existing documents, enter the following command:
mongoimport --db mydatabase --collection mycollection --type csv --file updatedata.csv --upsert
This command imports the data from the updatedata.csv
file into the mycollection
collection in the mydatabase
database. The --upsert
option tells MongoDB to update existing documents if a match is found, and to insert new documents if no match is found.
The MongoDB import tool is a powerful feature that makes it easy to import data into your MongoDB database. With the ability to control various options and file types, you can easily import data from a variety of sources to meet your specific needs.