Search code examples
pythonjsonmongodbpymongo

How do I filter out all documents in which a time field doesn't exist in PyMongo?


I am using the sample_airbnb database on MongoDB cloud with the listingsAndReviews collection and I am comparing the first_review field. I tried selecting for the documents in which the first_review field exists by using the $exists operator but it isn't working.

the image of first_review

(I'm trying to check if the first_review field here exists before running calculations on that)

I tried selecting for the fields in which the query exists by using the following query with $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)

Instead I keep getting this error:

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

Solution

  • I had a look at your repo and you are not setting collection correctly; collection is a string. It needs to be a database collection.

    It doesn't fail harder because str implements a find method. You can repro your exact error message with:

    'a'.find({'b': 'c'})
    

    errors:

      File "...string2.py", line 1, in <module>
        'a'.find({'b': 'c'})
    TypeError: must be str, not dict