Oussama L.
Oussama L.

Reputation: 1912

add if no duplicate in collection mongodb python

i'm writing a script that puts a large number of xml files into mongodb, thus when i execute the script multiple times the same object is added many times to the same collection.

I checked out for a way to stop this behavior by checkinng the existance of the object before adding it, but can't find a way.

help!

Upvotes: 0

Views: 517

Answers (2)

shx2
shx2

Reputation: 64328

The term for the operation you're describing is "upsert".

In mongodb, the way to upsert is to use the update functionality with upsert=True.

Upvotes: 1

vmr
vmr

Reputation: 1935

You can index on one or more fields(not _id) of the document/xml structure. Then make use of find operator to check if a document containing that indexed_field:value is present in the collection. If it returns nothing then you can insert new documents into your collection. This will ensure only new docs are inserted when you re-run the script.

Upvotes: 0

Related Questions