Fast or Bulk Upsert in pymongo

前端 未结 6 1940
逝去的感伤
逝去的感伤 2020-11-29 02:07

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 h

6条回答
  •  误落风尘
    2020-11-29 02:53

    MongoDB 2.6+ has support for bulk operations. This includes bulk inserts, upserts, updates, etc. The point of this is to reduce/eliminate delays from the round-trip latency of doing record-by-record operations ('document by document' to be correct).

    So, how does this work? Example in Python, because that's what I'm working in.

    >>> import pymongo
    >>> pymongo.version
    '2.7rc0'
    

    To use this feature, we create a 'bulk' object, add documents to it, then call execute on it and it will send all the updates at once. Caveats: The BSONsize of the collected operations (sum of the bsonsizes) cannot be over the document size limit of 16 MB. Of course, the number of operations can thus vary significantly, Your Mileage May Vary.

    Example in Pymongo of Bulk upsert operation:

    import pymongo
    conn = pymongo.MongoClient('myserver', 8839)
    db = conn['mydbname']
    coll = db.myCollection
    bulkop = coll.initialize_ordered_bulk_op()
    retval = bulkop.find({'field1':1}).upsert().update({'$push':{'vals':1}})
    retval = bulkop.find({'field1':1}).upsert().update({'$push':{'vals':2}})
    retval = bulkop.find({'field1':1}).upsert().update({'$push':{'vals':3}})
    retval = bulkop.execute()
    

    This is the essential method. More info available at:

    http://api.mongodb.org/python/2.7rc1/examples/bulk.html

    Edit :- since version 3.5 of python driver, initialize_ordered_bulk_op is deprecated. Use bulk_write() instead. [ http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.bulk_write ]

提交回复
热议问题