Mongoimport json file update or overwritte ..?
Asked Answered
R

7

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
Ruella answered 21/4, 2015 at 14:39 Comment(0)
A
30

For MongoDB v3.x,

--mode=upsert
Accordion answered 21/4, 2015 at 15:13 Comment(0)
V
12

--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.

Vicky answered 14/2, 2018 at 6:32 Comment(1)
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.Neurovascular
W
7

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

But you can update it using --upsert flag.

Whatever answered 21/4, 2015 at 14:49 Comment(3)
ok, i want to erase and re-import so if i use upsert it'si good for me ?Ruella
you can use upsert which will overwrite the existing. (erase+ import = overwrite with new data.) Let me know if you need something different.Whatever
not working i use --upsert a the end of my command line but when i do a db.stats() the numbers of objects increaseRuella
T
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

Thurmanthurmann answered 2/7, 2019 at 8:28 Comment(0)
I
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

Interlay answered 29/7, 2020 at 2:5 Comment(0)
P
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".

Perchance answered 26/8, 2020 at 14:33 Comment(0)
S
0

I'm on version 1.40.3 and now you can finally do it without a CMD.

Update the data on the cell and click the UPDATE button on the right bottom.

If you have a lot of data, it may be hidden and you may have to scroll down to see it.

Where to find UPDATE button on MongoDB Compass

Snowfall answered 18/10, 2023 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.