I am using PyMongo to fetch documents from my collection. All was working fine, until I tried limit the results to only fetch documents where a specific field exists if a certain collection is selected. Here is my code
query["is_approved"] = None
if source_collection == "collection-name":
query["field_to_check_for"]['exists'] = True
sort = [("created_date", -1)]
cursor = c.find(query, sort=sort).limit(20)
The above code throws a 400 'bad request' error on the line with
query["field_to_check_for"]['exists'] = True
I have also tried using
query["field_to_check_for"] = "exists"
but that returns an empty result
your query dict is in wrong format, please try this one:
query = {"field_to_check_for": {"$exists": True}}
cursor = db.collection-name.find(query)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With