gpt4 book ai didi

python - pymongo.errors.InvalidOperation : cannot set options after executing query

转载 作者:行者123 更新时间:2023-12-03 08:41:48 27 4
gpt4 key购买 nike

我正在使用 Flask 和 pymongo 开发一个项目,我有一个用户集合和一个电影集合,其中实例如下:

 user = {"Email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f88d8b9d8ab89f95999194d69b9795" rel="noreferrer noopener nofollow">[email protected]</a>" , "Comments":[" Hobbit 2013 :good movie" , " Hobbit 2013 :bad movie" , " Spiderman 2020 :very good"]}

我还有一个电影集合,其中电影实例如下:

movie = {"title":"Hobbit" , "year":"2013" , "comments":["Hobbit 2013 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81f4f2e4f3c1e6ece0e8edafe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>:good movie"] }

我正在使用 jinja 2 模板向特定电影提交新标题,如果有任何用户评论了该电影,我希望每个评论该特定电影的用户在评论中都包含新电影名称前任。对于上述用户实例,霍比特人 -> 复仇者联盟

`user = {"Email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f085839582b0979d91999cde939f9d" rel="noreferrer noopener nofollow">[email protected]</a>" , "Comments":[" Avengers  2013 :good movie" , " Avengers 2013 :bad movie" , " Spiderman 2020 :very good"]}`  
`movie = {"title":"Avengers" , "year":"2013" , "comments":["Avengers 2013 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f0a0c1a0d3f18121e1613511c1012" rel="noreferrer noopener nofollow">[email protected]</a>:good movie"] }` #obviously a movie can have more comments from users not just one

使用 flask 端点,我成功更改了电影标题,但是我无法更改旧电影标题所在的任何用户评论,因为标题不会随我的代码变化

这是我的端点。我从 jinja2 表单中获取新标题,然后尝试用新标题替换注释中的旧标题。然后我收到错误

pymongo.errors.InvalidOperation: cannot set options after executing query

我的代码:

@app.route('/executemovieupdate' , methods = ['GET', 'POST'])
def execute_movie_update():
if 'Email' in session and 'User' in session:
email = session['Email']
user = session['User']
if user == 'Admin':
if 'Movie' in session and 'Movie_year' in session and 'Movie_plot' in session:
movie = session['Movie']
old_movie = str(movie) #store old movie title
print("old movie is " , old_movie)
year = session['Movie_year']
plot = session['Movie_plot']
tainia = movies.find_one({'title':movie , "year":year})
if request.method == 'POST':
new_title = request.form.get('new-title') #get the new title from jinja2 template



if new_title:
print("update the title")
movies.update_one({"title":movie , "year":year} , {"$set":{"title":new_title} } ) #the title is updated succesfully for the movie
session['Movie'] = new_title
movie = session['Movie']
user_list = users.find({})
for idxu, usr in enumerate(user_list): #this is where i try to change the title in the user comments
for idxc, comment in enumerate(usr['Comments']):
if old_movie in comment:
print("old comment here")
print(comment)
user_list[idxu]['Comments'][idxc] = comment.replace(old_movie ,new_title) #this is the line where the error occurs
print(comment) # the new comment is printed

return ''' movie has been updated '''

else:
return render_template('movie-update.html' , movie = tainia)
else:
return redirect(url_for('admin.html'))
else:
return redirect(url_for('login'))
else:
return redirect(url_for('login'))

非常感谢您对这个问题的帮助。预先感谢您。

最佳答案

如果你打印 type(user_list) 你会注意到它不是一个 dict 列表,而是一个 pymongo.cursor.Cursor 对象(是只读的),因此您可以将 user_list = users.find({}) 更改为 user_list = list(users.find({})) (但要小心它将查询集合users中的所有记录)并且它应该可以工作。

另一个更好的方法是使用 usr['Comments'][idxc] (这是一个 dict)而不是 user_list[idxu]['Comments '][idxc]

如果您想将该记录更新到 MongoDB,那么您将需要像上面一样使用 update_one

我没有你的数据,但类似这样的东西应该可以解决问题:

for idxu, usr in enumerate(user_list): #this is where i try to change the title in the user comments 
for idxc, comment in enumerate(usr['Comments']):
if old_movie in comment:
print("old comment here")
print(comment)
usr['Comments'][idxc] = comment.replace(old_movie ,new_title) #this is the line where the error occurs
print(comment) # the new comment is printed
users.update_one({"_id": usr['_id']}, {"$set": {"Comments": usr['Comments']}})

关于python - pymongo.errors.InvalidOperation : cannot set options after executing query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62550144/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com