Search code examples
mongodbpymongo-3.x

pymongo $exists :false vs False


I am reading data from pymongo and need to check if column exists or not

[doc for doc in col.find{"IAM":{"$exists":false}}  

I am getting and error near false

  • Tried so far:

I have tried to convert false to False, but in mongodb it is not returning anything. how to check if column exists in pymongo?


Solution

  • A few things to correct:

    • @R2D2 is correct; the syntax should be find({}).
    • The predicate should be False (capitalised) in python *
    • You can replace [for doc in doc ...] with just list()

    So your final statement is:

    result = list(col.find({"IAM": {"$exists": False}}))  
    

    *In the mongo shell / javascript it is false (lowercase)