0

Whats the behavior of Pymongo find_one? I would expect such a function to return a None or throw an exception when it does not find the required document. But it behaves like:

>>> q = db.find_one({'node_type': {'$regex':'impossible-condition'}})
>>> q
>>>
>>> q==1
False
>>> w==1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'w' is not defined

Does it return anything? And how to safely determine if the query did not match a value?

4

1 回答 1

0

由于find_one是集合级别的操作,因此您需要实际提供要查询的集合。以您为例,我们将查看 users 集合:

q = db.users.find_one({'node_type': {'$regex':'impossible-condition'}})

之后适用以下内容:find_one返回单个文档,如果没有找到匹配的文档,则返回 None。

见: http ://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find_one

于 2013-11-15T14:06:20.910 回答