📜  mongodb import (1)

📅  最后修改于: 2023-12-03 15:17:41.625000             🧑  作者: Mango

MongoDB Import

Introduction

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.

Syntax

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.

Options

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:

  • --db: Specifies the name of the database to which the data should be imported.
  • --collection: Specifies the name of the collection to which the data should be imported.
  • --type: Specifies the file format of the data being imported. This can be CSV, TSV, JSON, or BSON.
  • --file: Specifies the path to the file from which the data is being imported.
  • --headerline: Specifies that the first line of the file being imported should be treated as a header line.
  • --upsert: Specifies that the data being imported should be treated as either an update or an insert, depending on whether a matching document can be found in the collection.
  • --drop: Specifies that the target collection should be dropped before the data is imported.
  • --fields: Specifies a comma-separated list of fields to be imported.
  • --ignoreBlanks: Specifies that empty fields in the input file should be ignored.
Examples
Importing from a CSV File

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.

Importing from a JSON File

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.

Importing and Updating Documents

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.

Conclusion

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.