MongoDB - PyMONGO 更新 collection
MongoDB - PyMONGO update a collection
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 个操作 collection
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 部分有任何帮助,我们将不胜感激!
前两部分的 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
s 的列表,然后使用$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"}})
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 个操作 collection
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 部分有任何帮助,我们将不胜感激!
前两部分的 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
s 的列表,然后使用$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"}})