As of mongoimport
version 100.0.0, you can delete documents in MongoDB based on an imported file. To do this, use delete
mode.
When you use delete
mode, if an imported document has the same _id
value as an existing one in the collection that you’re importing into, the existing document will be deleted.
You can also specify another field or fields (other than the _id
field) to be the matching field if required.
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 } { "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 } { "_id" : 5, "name" : "Bruce", "type" : "Bat", "weight" : 3 }
And we have the following JSON file called pets.json
:
{"_id":1,"name":"Wag","type":"Dog"} {"_id":2,"name":"Fluffy","type":"Cat","weight":10} {"_id":9,"name":"Hop","type":"Kangaroo","weight":60}
The following command imports the JSON file using delete
mode:
mongoimport --db=PetHotel --collection=pets --mode=delete --file=data/pets.json
Running this command deletes any matching documents in the collection.
Check the Results
Let’s take a look at the collection now.
db.pets.find()
Result:
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 } { "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 } { "_id" : 5, "name" : "Bruce", "type" : "Bat", "weight" : 3 }
We can see that the first 2 documents have been deleted. They were deleted because their _id
values matched the _id
values in the existing documents. This is despite other details not necessarily matching (such as with document 2).
Also, our imported file had a document with an _id
of 9 but there was no matching document, and so nothing was deleted for that one.
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 multiple fields with this parameter, pass them as a comma separated list.
Suppose we have another JSON file called pets2.json
and it looks like this:
{ "name" : "Meow", "type" : "Cat", "weight" : 7 } { "name" : "Bubbles", "type" : "Fish", "weight" : 3 }
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=delete --upsertFields=name,type --file=data/pets2.json
Now let’s check the collection again:
db.pets.find()
Result:
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 } { "_id" : 5, "name" : "Bruce", "type" : "Bat", "weight" : 3 }
Meow the cat was deleted because that pet existed in both the MongoDB collection and the imported document.
Bubbles the fish had no matching document and so nothing was deleted for that one.
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.