How to Replace Existing Documents when Importing a File into MongoDB

When you use mongoimport to import files into MongoDB, you have the option of replacing existing documents that match the ones you’re importing.

By this I mean, if an imported document has the same _id value as an existing one in the collection you’re importing into, the existing document will be replaced with the one being imported.

You can also specify another field (other than the _id field) to be the matching field if required.

The way to replace existing documents when using mongoimport is to use upsert mode.

Continue reading

The Import Modes of mongoimport

When using mongoimport to import files into MongoDB, you have the option of specifying a mode to use. These modes determine what happens if there’s already matching documents in the collection that you’re trying to import into.

By default, mongoimport uses insert mode, but there are other modes you can use. The mode you use will depend on what you’re trying to do.

Below is an overview of each mode along with examples.

Continue reading