简体   繁体   中英

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 $and operator.

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

Both queries using compass I had no problem.

我认为你也可以这样做:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM