简体   繁体   English

pymongo 方法 find_one 有效,但 find 方法无效

[英]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.当我使用find方法尝试相同的查询时,返回为空。

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

My objective is a query expression with 2 arguments "name.first" : "victor" and "name.last":"perdensen" .我的目标是一个带有 2 个参数"name.first" : "victor""name.last":"perdensen"的查询表达式。 I also tried $and operator.我也试过$and运算符。

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.如果数据可用, find_one()返回一个dict ,而find()返回一个迭代器,因为可能有多个 json 文档。

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.因此,如果您使用find_one()获得查询结果,您也将使用find()获得结果,但这一次,您必须在 for 循环中访问它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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