19

I'm having DB which name is "Project" and collection which name is "sample" then I inserted one JSON file using mongoimport command.

Now i edited the same JSON file. So if want to import the same JSON file to the Collection then I am facing the problem like multiple instances are created and no updating is not taking place.

Is there any way to update or overwrite the data already present in the mongodb using mongoimport command ?

Note that I also tried using --mode=upsert flag:

./mongoimport --db Project --collection sample --mode=upsert --file   /home/rule.json
Mercer
  • 9,736
  • 30
  • 105
  • 170

6 Answers6

30

For MongoDB v3.x,

--mode=upsert
evandrix
  • 6,041
  • 4
  • 27
  • 38
lanenok
  • 2,699
  • 17
  • 24
11

--drop flag also can be used along with mongoimport command to overwrite/update the existing data.

--drop


./mongoimport --db Project --collection sample --drop --file   /home/UCSC_rule.json

I gave this solution because i have tried using --upsert flag but i could not see any changes in the existing data instead new instance was created.

Abdulvakaf K
  • 606
  • 1
  • 7
  • 16
  • 2
    Note that `--drop` remove the whole collection before importing the data from the json file, while `--mode=upsert` insert the new keys and updates the existing ones. – Nicolas Payart May 15 '19 at 13:43
7

Default behavior says skip if already exists so by default it wont overwrite existing data.

But you can update it using --upsert flag.

Nachiket Kate
  • 8,473
  • 2
  • 27
  • 45
  • ok, i want to erase and re-import so if i use `upsert` it'si good for me ? – Mercer Apr 21 '15 at 14:53
  • you can use upsert which will overwrite the existing. (erase+ import = overwrite with new data.) Let me know if you need something different. – Nachiket Kate Apr 21 '15 at 14:55
  • 1
    not working i use --upsert a the end of my command line but when i do a db.stats() the numbers of objects increase – Mercer Apr 21 '15 at 15:07
2

Based on the mongo doc, the reason --mode=upsert doesn't work in your case is by default, mongoimport uses the _id field. So --drop should be the correct answer.

--mode=upsert:

By default, mongoimport uses the _id field to match documents in the collection with documents in the import file. To specify the fields against which to match existing documents for the upsert and merge modes, use --upsertFields.

--drop:

Modifies the import process so that the target instance drops the collection before importing the data from the input.

mongoimport document

1

--mode upsert will create a new record if _id's don't match. These _id's are preserved across db's on different machines by mongodump/mongorestore

--drop will delete the entire collection!

To overwrite one or more records without dropping other existing records, ensure the id's match when using --upsert

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
Gavin
  • 131
  • 5
0

Using MongoDb version 3.6.8 the following solution worked to me

mongoimport --db <db name> --collection <collection name> --upsert --upsertFields <field name>  --file <path to json file> --jsonArray

Where "field name" was the name of key used by MongoDb to compare instead of the default field "_id".

Dharman
  • 30,962
  • 25
  • 85
  • 135