Catherine Nosova
Catherine Nosova

Reputation: 109

Pymongo find returns empty result when I put boolean fields into query

everyone!

I have mongodb collections "Products" like this example:

{
 "_id" : "0000",
 "product_id" : "XXX"
 "price" : 100,
 "quantity" : 10,
 "available" : true
}

And some code on python:

from pymongo import MongoClient
client = MongoClient()
mongo = client.db.products
requests = mongo.find({'quantity' : {'$gt' : 50}, 
                       'available' : 'true'}, 
                      {'product_id' : 1, '_id' : 0})
selling_profile = [obj["product_id"] for obj in requests]

This query returns the empty list, but I know that there are available products with quantity greater than 50 in database.

When I try to search only with condition on quantity, the result is not empty. I suspect there's some problem with boolean field in my pymongo code.

Can someone help me with this issue, please?

Upvotes: 1

Views: 3978

Answers (1)

Evhz
Evhz

Reputation: 9246

Do it like this:

from pymongo import MongoClient
client = MongoClient()
mongo = client.db.products
requests = mongo.find({'quantity' : {'$gt' : 50}, 
                       'available' : True}, 
                      {'product_id' : 1, '_id' : 0})
selling_profile = [obj["product_id"] for obj in requests]

by using the boolean Python notation.

Upvotes: 4

Related Questions