Home > OS >  Pymongo find(): Having issues filtering data
Pymongo find(): Having issues filtering data

Time:03-03

I am trying to filter out data from a collection based on a particular id, but looks like they are not really filtered out.

My code looks something like this:

myid = '100'
for x in collection.find({},{"myid": myid}):
    print(x)

I am not sure if I am going wrong with the syntax somewhere, can someone please help.

CodePudding user response:

You can try the following way out

myid = '100'
for x in list(collection.find({"myid": myid})):
    print(x)

Since only collection.find() will return you a pymongo object. So, in order to make that object iterable you have to convert it into a list.

CodePudding user response:

The first two parameters to .find() are a filter and a projection; you have passed your filter to the projection parameter. You just need this:

myid = '100'
for x in collection.find({"myid": myid}):
    print(x)

There is no need to convert it explicitly to a list as .find() returns an iterable object already.

  • Related