我正在使用 MongoDB 云上的sample_airbnb 数据库和listingsAndReviews 集合,并且我正在比较first_review 字段。我尝试使用 $exists 运算符选择存在 first_review 字段的文档,但它不起作用。
(我正在尝试在对其进行计算之前检查此处的first_review字段是否存在)
我尝试通过使用以下带有 $exists 的查询来选择查询存在的字段:
# Query documents where the first_revi9ew field exists, sort in ascending order, and limit to 100
cursor = collection.find({"first_review": {"$exists": True}})
cursor.sort("first_review", pymongo.ASCENDING).limit(100)
相反,我不断收到此错误:
Traceback (most recent call last):
File "...\reading\mongodbReader.py", line 61, in read_mongodb
cursor = collection.find({"first_review": {"$exists": True}})
TypeError: must be str, not dict
我查看了您的存储库,您没有正确设置
collection
; collection
是一个字符串。它需要是一个数据库集合。
它不会更失败,因为
str
实现了 find 方法。您可以通过以下方式重现您的确切错误消息:
'a'.find({'b': 'c'})
错误:
File "...string2.py", line 1, in <module>
'a'.find({'b': 'c'})
TypeError: must be str, not dict