Forrest logo
back to the mongoimport tool

mongoimport:tldr:596dd

mongoimport: Import a JSON file using a specific mode and a query to match existing documents.
$ mongoimport --file=${filename-json} --mode=${select} --upsertFields="${field1,field2,---}"
try on your machine

The given command is used to import data into a MongoDB database using the mongoimport tool.

Here is the breakdown of the command:

  • mongoimport: This is the command itself, invoking the mongoimport tool to perform the import operation.

  • --file=${filename-json}: Specify the path or filename of the JSON file from which the data will be imported. ${filename-json} is a placeholder for the actual JSON file you want to import.

  • --mode=${select}: This option indicates the mode of import. ${select} is another placeholder, which should be replaced with one of the available modes: 'insert', 'upsert', 'merge', or 'upsertReplace'.

    • 'insert' mode inserts new documents to the collection.

    • 'upsert' mode checks if a document already exists with the specified fields (defined by --upsertFields), and if found, updates the document; otherwise, it inserts a new document.

    • 'merge' mode merges the imported documents with the existing documents in the collection, either updating the fields of existing documents or inserting new documents.

    • 'upsertReplace' mode replaces existing documents if found based on the --upsertFields; otherwise, it inserts new documents.

  • --upsertFields="${field1,field2,---}": This option specifies the fields used to determine if a document already exists during an upsert operation. "${field1,field2,---}" is a placeholder for the fields separated by commas. The tool will check for the existence of a document based on these fields before deciding whether to update or insert.

Make sure to replace the placeholders with the actual values before executing the command.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the mongoimport tool