Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert or ignore multiple documents in mongoDB

I have a collection in which all of my documents have at least these 2 fields, say name and url (where url is unique so I set up a unique index on it). Now if I try to insert a document with a duplicate url, it will give an error and halt the program. I don't want this behavior, but I need something like mysql's insert or ignore, so that mongoDB should not insert the document with duplicate url and continue with the next documents.

Is there some parameter I can pass to the insert command to achieve this behavior? I generally do a batch of inserts using pymongo as:

collection.insert(document_array)

Here collection is a collection and document_array is an array of documents.

So is there some way I can implement the insert or ignore functionality for a multiple document insert?

like image 582
lovesh Avatar asked Apr 30 '12 18:04

lovesh


2 Answers

Use insert_many(), and set ordered=False.

This will ensure that all write operations are attempted, even if there are errors: http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.insert_many

like image 64
barlaso Avatar answered Nov 15 '22 20:11

barlaso


Try this:

try:
    coll.insert(
        doc_or_docs=doc_array,
        continue_on_error=True)
except pymongo.errors.DuplicateKeyError:
    pass

The insert operation will still throw an exception if an error occurs in the insert (such as trying to insert a duplicate value for a unique index), but it will not affect the other items in the array. You can then swallow the error as shown above.

like image 32
William Payne Avatar answered Nov 15 '22 18:11

William Payne