Reputation: 556
I am a beginner in pymongo and flask and I have a movies collection where a movie instance is like :
movie = { "title": "Hobbit", "year": "2013", "plot": "some plot" }
I am trying to iterate through my whole movies collection to check if a movie I have submitted in a form exists inside the collection . The movie title and year have to be the same . My code :
movie_list = movies.find()
for i in movie_list:
for t in i['title']:
for y in i['year']:
if request.form['movie'] == t and request.form['year'] == y:
print("found")
break
However, when I test the above code using the print statement it prints nothing even if the movie and year I have submitted exist in my mongodb collection. I would appreciate your help in guiding me to solve this simple task. Thank you in advance
Upvotes: 0
Views: 87
Reputation: 1435
If you aim just to check the existence of your movie, it's better to do it like this:
count = movies.count_documents({"title": request.form["movie"], "year": request.form["movie"]})
if count:
print("found")
If you want to do it with a loop,
movies = movies.find({"title": request.form["movie"], "year": request.form["movie"]})
for movie in movies:
if movie["title"] = request.form["movie"] and movie["year"] = request.form["year"]:
print("found")
Upvotes: 1