How to use the command mongoimport (with examples)

How to use the command mongoimport (with examples)

Mongoimport is a command-line tool that allows users to import content from a JSON, CSV, or TSV file into a MongoDB database. It provides various options for importing data, including specifying the file format, collection name, field names, and import modes. This article will illustrate each of these use cases with examples.

Use case 1: Import a JSON file into a specific collection

Code:

mongoimport --file=path/to/file.json --uri=mongodb_uri --collection=collection_name

Motivation: This use case is useful when you want to import a JSON file into a specific collection in your MongoDB database. By specifying the file path, MongoDB URI, and collection name, you can easily import the JSON content.

Explanation:

  • --file: Specifies the path to the JSON file to be imported.
  • --uri: Specifies the MongoDB connection URI.
  • --collection: Specifies the name of the collection that the JSON file will be imported into.

Example output:

2021-07-01T10:00:00.000+0000    connected to: mongodb_uri
2021-07-01T10:00:00.000+0000    imported 100 documents into collection_name

Use case 2: Import a CSV file, using the first line of the file to determine field names

Code:

mongoimport --type=csv --file=path/to/file.csv --db=database_name --collection=collection_name

Motivation: In many cases, CSV files have the first line as the header with field names. This use case allows you to import a CSV file into a specific collection while using the first line of the file to determine the field names automatically.

Explanation:

  • --type: Specifies the file format as CSV.
  • --file: Specifies the path to the CSV file to be imported.
  • --db: Specifies the name of the database where the collection belongs.
  • --collection: Specifies the name of the collection that the CSV file will be imported into.

Example output:

2021-07-01T10:00:00.000+0000    connected to: mongodb_uri
2021-07-01T10:00:00.000+0000    imported 1000 documents into collection_name

Use case 3: Import a JSON array, using each element as a separate document

Code:

mongoimport --jsonArray --file=path/to/file.json

Motivation: Sometimes, you may have a JSON file that contains an array of objects, and you want each element to be imported as a separate document in MongoDB. This use case allows you to achieve that by specifying the --jsonArray flag.

Explanation:

  • --jsonArray: Indicates that the JSON file contains an array of documents.

Example output:

2021-07-01T10:00:00.000+0000    connected to: mongodb_uri
2021-07-01T10:00:00.000+0000    imported 5 documents

Use case 4: Import a JSON file using a specific mode and a query to match existing documents

Code:

mongoimport --file=path/to/file.json --mode=delete|merge|upsert --upsertFields="field1,field2,..."

Motivation: In some situations, you may need to update your MongoDB collection based on an existing JSON file. This use case allows you to specify a mode (delete, merge, or upsert) and fields to match existing documents, so you can control how the new data interacts with the existing data.

Explanation:

  • --mode: Specifies the import mode (delete, merge, or upsert).
  • --upsertFields: Specifies the fields that should match existing documents for the specified import mode.

Example output:

2021-07-01T10:00:00.000+0000    connected to: mongodb_uri
2021-07-01T10:00:00.000+0000    imported 500 documents, updated 200 existing documents

Use case 5: Import a CSV file, reading field names from a separate CSV file and ignoring fields with empty values

Code:

mongoimport --type=csv --file=path/to/file.csv --fieldFile=path/to/field_file.csv --ignoreBlanks

Motivation: If you have a situation where the field names for a CSV file are stored in a separate CSV file, this use case allows you to read the field names from that separate file. Additionally, the --ignoreBlanks flag ensures that fields with empty values in the CSV file are ignored during the import.

Explanation:

  • --fieldFile: Specifies the path to the CSV file that contains the field names.
  • --ignoreBlanks: Ignores fields with empty values in the CSV file during import.

Example output:

2021-07-01T10:00:00.000+0000    connected to: mongodb_uri
2021-07-01T10:00:00.000+0000    imported 1000 documents into collection_name

Use case 6: Display help

Code:

mongoimport --help

Motivation: When you need to quickly access information about the available options and usage of the mongoimport command, you can use the --help flag to display the help menu.

Example output:

Usage: mongoimport <options>

Options:
  --file=<filename>                         The file to import from; must be JSON, CSV, or TSV format
  --uri=<connectionString>                   mongodb URI connection string
  --collection=<collectionName>             The collection to import to; if not specified, uses file/basename
  --type=<fileType>                          Type of file to import: json, csv, or tsv (defaults to json)
  --db=<databaseName>                        Database to import to (defaults to test)
  --jsonArray                                Treat input file as a JSON array, each element as a separate document
  --mode=<importMode>                        Mode of import: delete, merge, or upsert (defaults to insert)
  --upsertFields=<fields>                    Fields for upsert operation
  --fieldFile=<filename>                     Specify the field names file. CSV format is expected. (Only for CSV)
  --ignoreBlanks                             Ignore fields with empty values in the CSV file (Only for CSV)
...

Conclusion:

The mongoimport command is a versatile tool for importing content from JSON, CSV, or TSV files into a MongoDB database. It offers various options and modes for importing data, making it flexible for different use cases. By following the examples in this article, you can easily import data into your MongoDB collections and control how the new data interacts with existing documents.

Related Posts

How to use the command `doxygen` (with examples)

How to use the command `doxygen` (with examples)

Doxygen is a documentation system that is used to generate documentation for various programming languages.

Read More
How to use the command 'strip-nondeterminism' (with examples)

How to use the command 'strip-nondeterminism' (with examples)

The ‘strip-nondeterminism’ command is a tool used to remove non-deterministic information, such as timestamps, from files.

Read More
Command-Line Prolog Interpreter: swipl (with examples)

Command-Line Prolog Interpreter: swipl (with examples)

Introduction SWI-Prolog is a comprehensive and widely used Prolog environment. It provides an interactive shell where you can execute Prolog commands, scripts, and perform various configurations.

Read More