python 操作mongodb进行批量update某一字段

import pymongo


myclient = pymongo.MongoClient(host='localhost',port=27017)
mydb = myclient["du_brand"]
mycol = mydb["content"]
mycol2 = mydb["content_new"]

f1 = mycol.find({"color":{"$exists":True}})
f11 = mycol2.find({"color": {"$exists": True}})

L1 = []
L2 = []
for a1 in f1:
    id1 = a1['id']
    L1.append(a1)

for a11 in f11:
    id11 = a11['id']
    L2.append(a11)

for i in L1:
    for j in L2:
        if i['id'] == j['id']:
            print i['color'],j['color']
            color = j["color"]
            mycol.update({"_id":i["_id"]},{"$set":{"color":color}})
 

猜你喜欢

转载自blog.csdn.net/Gu_zhi/article/details/85637920