python - mongoengine bulk upsert a batch of record? -


i want upsert(update or insert) list of record, in fact know mongodb support bulk operation since mongodb3.0.

i want know whether mongoengine support bulk upsert operation in mongoengine (0.10.0).

if not, want know how upsert list of record, know mongoengine support insert batch this:

class user(document):     username = stringfield(required=true)     password = stringfiedl(required=true)     meta = {'db_alias': 'user_info', 'collection': 'user',               'indexes': [{'fields': ['username'], 'unique': true}]            }  def save_users(self, users):     users.objects.insert(users) # raise mongoengine.errors.notuniqueerror 

you can use bulk operations api directly accessing underlying collection object pymongo driver mongoengine uses. mongodb has supported bulk operations since version 2.6. there newer methods access these since v3 of pymongo driver, basic methods have been around since corresponding driver update 2.6 server version ( pymongo 2.7 ).

to mongoengine can call undocumented ._get_collection() class return collection object:

bulk = users._get_collection().initialize_ordered_bulk_op()  user in users:  # users list of dicts containing data work on     bulk.find({ "matchfield": user['matchfield'] }).upsert().replace_one(user)  bulk.execute() 

or other usage of bulk methods such .update_one() want. , .upsert() chained method modifies such update statements.

you working raw python objects here there no direct equivalent in mongoengine itself. can use operations accessing methods underlying driver


Comments

Popular posts from this blog

jquery - ReferenceError: CKEDITOR is not defined -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -