Mongoengine: Atomic [create or modification]

  Kiến thức lập trình

I have the following MongoDB document-class defined with mongoengine:

class Archive( mongoengine.Document ):
    user_id         = mongoengine.StringField()
    transaction_ids = mongoengine.ListField( default=list )

I want this class to operate similarly to a Python object that could be defined as: archive = collections.defaultdict(list), where it is trivial to say archive[a_user_id].append(a_transaction_id)

The tricky part is that I would like this to operate atomically with respect to the MongoDB. I have been around and around with the mongoengine documentation and can’t put together what I need, nor find examples.

In my naive mind, I am thinking of pseudocode like this:

define atomically_archive_transaction( a_user_id, a_transaction_id ):
    Archive.atomically_create_or_modify(
        query = { user_id=a_user_id },
        create_if_does_not_exist = { user_id=a_user_id, transaction_ids=[a_transaction_id,] },
        modify_if_does_exist = { push__transaction_id=a_transaction_id },
        write_to_disk_afterwards = 'all in the same atomic transaction',
        )

Is there any hope?

As a perhaps more tractable approach, it would be ok to try to always create a new document for a_user_id and have the creation fail if the document already exists. (Not clear how to do that.) Then I could simply follow with an atomic push, which would avoid possible race conditions, but I’m then left with the challenge of how to make sure the save occurs in the same atomic operation as the push. (The documentation here is not as clear as it could be on this point; it only implies that the save-to-disk occurs in its example: BlogPost.objects(id=post.id).update_one(push__tags='nosql') )

Thank you.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT