[英]MongoDB - PyMONGO update a collection
收藏:
{'_id': ObjectId('606df83ba9f0f494a6cf4e19'), 'UID': 'tt0069204', 'TYPE': 'movie', 'startYear': 2003}
{'_id': ObjectId('606df83ba9f0f494a6cf4e20'), 'UID': 'tt0085953', 'TYPE': 'movie', 'startYear': 2003}
{'_id': ObjectId('606df83ba9f0f494a6cf4e21'), 'UID': 'tt0069049', 'TYPE': 'movie', 'startYear': 2003}
{'_id': ObjectId('606df83ba9f0f494a6cf4e22'), 'UID': 'tt0091490', 'TYPE': 'movie', 'startYear': 2003}
{'_id': ObjectId('606df83ba9f0f494a6cf4e23'), 'UID': 'tt0100275', 'TYPE': 'movie', 'startYear': 2003}
我需要對這個集合執行 3 次操作
1. Update the TYPE to be tvSeries for where the startyear = 2003
2. Select the documents from the productions collection where the TYPE = tvSeries
3. Using the UID value of the records whose TITLE_TYPE you edited above,
change the TYPE ‘realityShow’.
我被困在第三部分。 不確定問題的第三部分是否有效。
對第 3 部分的任何幫助將不勝感激!
前 2 部分的 mycode
import pymongo
import pprint
client=pymongo.MongoClient("mongodb://localhost:27017/")
db.client.database
collection=db.collection
myquery={"startYear":2003}
newvalues={"$set": {"TYPE":"tvSeries"}}
collection.update_many(myquery,newvalues)
for x in collection.find({"TYPE":"tvSeries"}).limit(5):
pprint.pprint(x)
獲取第一次更新前的UID
列表,然后使用$in
執行第二次更新:
myquery = {"startYear": 2003}
newvalues = {"$set": {"TYPE": "tvSeries"}}
uids = [x.get('UID') for x in collection.find(myquery, {'UID': 1})]
collection.update_many(myquery, newvalues)
for x in collection.find({"TYPE": "tvSeries"}).limit(5):
pprint.pprint(x)
collection.update_many({'UID': {'$in': uids}}, {"$set": {"TYPE": "realityShow"}})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.