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.

Example

Suppose we have a collection called pets that contain the following documents:

db.pets.find()

Result:

{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }

And we have the following JSON file called pets.json:

{"_id":2,"name":"Fetch","type":"Dog"}
{"_id":3,"name":"Scratch","type":"Cat","weight":10}
{"_id":4,"name":"Hop","type":"Kangaroo","weight":60}

The following command imports the JSON file into the collection:

mongoimport --db=PetHotel --collection=pets --mode=upsert --file=data/pets.json

This uses upsert mode to replace matching documents with the imported ones.

Check the Results

Let’s take a look at the collection now.

db.pets.find()

Result:

{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Fetch", "type" : "Dog" }
{ "_id" : 3, "name" : "Scratch", "type" : "Cat", "weight" : 10 }
{ "_id" : 4, "name" : "Hop", "type" : "Kangaroo", "weight" : 60 }

We can see that document 2 has been completely replaced. The dog Bark has been replaced with the dog Fetch, and no weight is recorded for Fetch.

Document 3 has also been replaced, and document 4 is a completely new document altogether.

Change the Upsert Field/s

You can use the --upsertFields parameter to specify a field other than _id for which to match against. When using this parameter, pass the fields as a comma separated list.

Suppose we have another JSON file called pets2.json that we want to import, and it looks like this:

{"name":"Fetch","type":"Dog","weight":20,"gooddog":true}
{"name":"Scratch","type":"Cat","weight":15}
{"name":"Bubbles","type":"Fish"}

This document doesn’t include the _id field, so we would need to match against other fields that uniquely identify each document. In this case we could use the name and type fields.

We can therefore use the following mongoimport command:

mongoimport --db=PetHotel --collection=pets --mode=upsert --upsertFields=name,type --file=data/pets2.json

Now let’s check the collection again:

db.pets.find()

Result:

{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Fetch", "type" : "Dog", "weight" : 20, "gooddog" : true }
{ "_id" : 3, "name" : "Scratch", "type" : "Cat", "weight" : 15 }
{ "_id" : 4, "name" : "Hop", "type" : "Kangaroo", "weight" : 60 }
{ "_id" : ObjectId("5ff00800d99141016941217c"), "name" : "Bubbles", "type" : "Fish" }

We can see that Fetch has been updated/replaced (but the _id value remains the same). The same with Scratch.

Bubbles on the other hand, had no match for the upsert fields and was therefore inserted.

Check for mongoimport

mongoimport is part of the MongoDB Database Tools package. The MongoDB Database Tools are a suite of command-line utilities for working with MongoDB.

If you’re not sure whether you have the MongoDB Database Tools/mongoimport installed, try running the following command in your Terminal or Command Prompt to check:

mongoimport --version

If you have it, you should see version information, etc. If you don’t have it, you can use the installation instructions over at the MongoDB website to install it on to your system.

Where to Run the Commands?

Don’t forget, you need to run mongoimport commands from your system’s command line (e.g. a new Terminal or Command Prompt window).

Don’t run them from the mongo shell.