pymongo中'addToSet'&'pull'简单使用

pymongo中'addToSet'&'pull'简单使用

def update_malicious_sample_advantage_tags(self, update_tags_dict, opr_type):
    """
    批量更新恶意文件样本库中的优势样本标签
    :param update_tags_dict:
    :param opr_type:
    :return:
    """
    now = datetime.now()
    update_tags_list = list()
    for sample_id in update_tags_dict:
        filter_dict = {
            "is_deleted": 0,
            "_id": ObjectId(sample_id)
        }
        if opr_type:
            # 将欲更新的标签列表中的每一个标签ID添加到样本的标签列中
            update_dict = {
                "$addToSet": {
                    "tags": {
                        "$each": update_tags_dict[sample_id]
                    }
                },
                "$set": {
                    "update_time": now
                }
            }
        else:
            # 样本原标签列中的标签ID凡是在欲更新的标签列表中的,全部清除
            update_dict = {
                "$pull": {
                    "tags": {
                        "$in": update_tags_dict[sample_id]
                    }
                },
                "$set": {
                    "update_time": now
                }
            }
      
        update_tags_list.append(UpdateOne(filter_dict, update_dict))
        
    # 批量更新样本的标签信息
    self.malicious_sample_col.bulk_write(update_tags_list)
抟扶摇而上者九万里
原文地址:https://www.cnblogs.com/fengting0913/p/14638280.html