I have the following python code:
placeholder = db.user.aggregate(...)
if len(list(placeholder))!=0:
#execute something
Now the issue is even if the mongo cursor doesn't have any documents, it still getting inside the if statement. But if I am modifying the code as:
placeholder = list(db.user.aggregate(...))
if len(placeholder)!=0:
#execute something
its working normally. even if pymongo cursor object is an iterator and it can be exhausted but the only thing I am doing is checking the length and converting it into a list. Can anyone please advice why its behaving this way in the first case. Thanks in advance.
CodePudding user response:
As long as the query is the same, the two statements are identical.
In the first query - how do you know there's no records. You exhaust the cursor without saving any results to look at. Also how is it possible that "the mongo cursor doesn't have any documents", while len(list(cursor))
returns a non-zero value.
If you are really stuck, post a Minimal Reproducible Example.