'Update an existing document.
Helper method, should only be used inside save().'

def _save_update(self, doc, save_condition, write_concern):

    collection = self._get_collection()
    object_id = doc['_id']
    created = False
    select_dict = {}
    if (save_condition is not None):
        select_dict = transform.query(self.__class__, **save_condition)
    select_dict['_id'] = object_id
    shard_key = self._meta.get('shard_key', tuple())
    for k in shard_key:
        path = self._lookup_field(k.split('.'))
        actual_key = [p.db_field for p in path]
        val = doc
        for ak in actual_key:
            val = val[ak]
        select_dict['.'.join(actual_key)] = val
    update_doc = self._get_update_doc()
    if update_doc:
        upsert = (save_condition is None)
        last_error = collection.update(select_dict, update_doc, upsert=upsert, **write_concern)
        if ((not upsert) and (last_error['n'] == 0)):
            raise SaveConditionError('Race    condition    preventing    document    update    detected')
        if (last_error is not None):
            updated_existing = last_error.get('updatedExisting')
            if (updated_existing is False):
                created = True
    return (object_id, created)
