If I try find() something, how can I find out if something was found or not?

If I try find() something, how can I find out if something was found or not?

When I use find_one() it’s None , what about find() ?

Hi @Fungal_Spores welcome to the community!

I’m assuming you’re using Pymongo since you mentioned None.

find() returns an iterable cursor, so if there is nothing to return, calling next() on the cursor will raise StopIteration:

>>> cur = db.test.find()
>>> cur.next()
Traceback (most recent call last):
...
StopIteration

Alternatively, if you put the result set into a list, it will be an empty list:

>>> list(db.test.find())
[]

Best regards,
Kevin

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.