Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast or Bulk Upsert in pymongo

How can I do a bulk upsert in pymongo? I want to Update a bunch of entries and doing them one at a time is very slow.

The answer to an almost identical question is here: Bulk update/upsert in MongoDB?

The accepted answer doesn't actually answer the question. It simply gives a link to the mongo CLI for doing import/exports.

I would also be open to someone explaining why doing a bulk upsert is no possible / no a best practice, but please explain what the preferred solution to this sort of problem is.

like image 607
ComputationalSocialScience Avatar asked Mar 13 '11 21:03

ComputationalSocialScience


1 Answers

Modern releases of pymongo ( greater than 3.x ) wrap bulk operations in a consistent interface that downgrades where the server release does not support bulk operations. This is now consistent in MongoDB officially supported drivers.

So the preferred method for coding is to use bulk_write() instead, where you use an UpdateOne other other appropriate operation action instead. And now of course it is preferred to use the natural language lists rather than a specific builder

The direct translation of the old documention:

from pymongo import UpdateOne  operations = [     UpdateOne({ "field1": 1},{ "$push": { "vals": 1 } },upsert=True),     UpdateOne({ "field1": 1},{ "$push": { "vals": 2 } },upsert=True),     UpdateOne({ "field1": 1},{ "$push": { "vals": 3 } },upsert=True) ]  result = collection.bulk_write(operations) 

Or the classic document transformation loop:

import random from pymongo import UpdateOne  random.seed()  operations = []  for doc in collection.find():     # Set a random number on every document update     operations.append(         UpdateOne({ "_id": doc["_id"] },{ "$set": { "random": random.randint(0,10) } })     )      # Send once every 1000 in batch     if ( len(operations) == 1000 ):         collection.bulk_write(operations,ordered=False)         operations = []  if ( len(operations) > 0 ):     collection.bulk_write(operations,ordered=False) 

The returned result is of BulkWriteResult which will contain counters of matched and updated documents as well as the returned _id values for any "upserts" that occur.

There is a bit of a misconception about the size of the bulk operations array. The actual request as sent to the server cannot exceed the 16MB BSON limit since that limit also applies to the "request" sent to the server which is using BSON format as well.

However that does not govern the size of the request array that you can build, as the actual operations will only be sent and processed in batches of 1000 anyway. The only real restriction is that those 1000 operation instructions themselves do not actually create a BSON document greater than 16MB. Which is indeed a pretty tall order.

The general concept of bulk methods is "less traffic", as a result of sending many things at once and only dealing with one server response. The reduction of that overhead attached to every single update request saves lots of time.

like image 157
Neil Lunn Avatar answered Oct 10 '22 17:10

Neil Lunn