I am new to MongoDB I am trying to import the JSON file from my local to MongoDB using the command
mongoimport --db testingdb --collection dbcollection1 --file books.json --jsonArray
It appends the data to the collection perfectly. But when I edit some documents in the same JSON file and redo the command instead of updating the docs it appends the same data again. So how can I added the docs to the collection by updating the docs already present in the db?
As stated, the --upsert
and --upsertFields
options handle this. The latter is used when a field or fields other than _id
determine how to match the document.
In your case:
mongoimport --db testingdb --collection dbcollection1 \
--upsert --upsertFields recipe_name \
--file books.json --jsonArray
And if a match is found for that field in the collection then the data present will be overwritten by the imported data.