Append item to MongoDB document array in PyMongo without re-insertion

You don’t need to use to retrieve the document first just use the .update method with the $push operator.

def update_tags(ref, new_tag):
    coll.update({'ref': ref}, {'$push': {'tags': new_tag}})

Since update is deprecated you should use the find_one_and_update or the update_one method if you are using pymongo 2.9 or newer

Leave a Comment