mongo批量操作存在更新否则插入

def save_data(ok_ps):
    ns = []
    for ok in ok_ps:
        ok['last_use_time'] = 0
        ok['protocol'] = 0  # 协议类型 0:http
        ok['is_own'] = 1
        ok['is_ok'] = 0  # 可用
        ok['date'] = datetime.datetime.utcnow()

        ns.append(ReplaceOne({'ip': ok['ip']}, ok, upsert=True))

    if len(ns) > 0:
        res = coll.bulk_write(ns)  
posted @ 2018-10-19 09:55  公众号python学习开发  阅读(1366)  评论(1编辑  收藏  举报