Ennio Ferreira
Ennio Ferreira

Reputation: 13

pymongo method find_one work but find method doesn't

I'm trying a simple query expression:

df = db.persons.find_one({"name.first": "victor"})

That's work fine. When I try the same query using find method the return is empty.

df = db.persons.find({"name.first":"victor"})

My objective is a query expression with 2 arguments "name.first" : "victor" and "name.last":"perdensen". I also tried $andoperator.

df = db.persons.find({"$and": [{"name.first": "victor"}, {"name.last": "pedersen"}]})

Both queries using compass I had no problem.

Upvotes: 1

Views: 717

Answers (2)

SeattleStephens
SeattleStephens

Reputation: 587

I think you can also do this:

df = list(db.persons.find({"name.first": "victor"}))

Upvotes: 1

ramazan polat
ramazan polat

Reputation: 7910

While find_one() returns a dict if data is available, find() returns an iterator since there may be more than one json document.

So if you get a result of a query with find_one(), you will also get a result with find() but this time, you have to access it in a for loop.

df = db.persons.find({"name.first": "victor"})
for df1 in df:
    print(df1) # here you get the result

Upvotes: 1

Related Questions