Update Mongoimport json file or overwrite.?

I have a database called โ€œProjectโ€ and a collection that is โ€œsampleโ€, after which I inserted a single JSON file using the mongoimport .

Now I edited the same JSON file. Therefore, if you want to import the same JSON file into the collection, I run into a problem, since several instances are created and the update does not occur.

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

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

 ./mongoimport --db Project --collection sample --mode=upsert --file /home/rule.json 
+11
source share
4 answers

For MongoDB v3.x,

 --mode=upsert 
+15
source

The default behavior indicates skipping if it already exists, so by default it will not overwrite existing data.

But you can update it using the --upsert flag.

+4
source
Flag

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

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

I gave this solution because I tried to use the - upsert flag, but I could not see any changes in the existing data, a new instance was created instead.

+4
source

Based on the mongo document, the reason --mode = upsert does not work in your case by default, mongoimport uses the _id field. Therefore --drop should be the right answer.

--mode = upsert:

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

--drop:

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

mongo import document

0
source

Source: https://habr.com/ru/post/985608/


All Articles