简体   繁体   English

当值在子字典中时,PyMongo查找字典值

[英]PyMongo Look up Dictionary Value when Value is in Sub-dictionary

I'm a total newbie to both Python and MongoDB, so please excuse what might be a silly question. 我是Python和MongoDB的新手,所以请原谅一个可能很愚蠢的问题。

I Have the record below in MongoDB. 我在MongoDB中有下面的记录。 Lars Torsk being the first name and last name of the record. Lars Torsk是记录的名字和姓氏。 Gunnar Torsk being the first name and last name of Lars' father. Gunnar Torsk是拉斯父亲的名字和姓氏。

{
    '_id': ObjectId('54840b59b6a1b322b042bde0'), 
    'First Name': 'Lars', 
    'Father': {'Last Name': 'Torsk', >'First Name': 'Gunnar', '_id': ObjectId('54840b59b6a1b322b042bddf')}, 
    'Aliases': ['rass'], 
    'Last Name': >'Torsk'
}

If I wanted to find all records of people with the first name Lars I would use: 如果我想查找所有名字叫Lars的人的记录,我将使用:

for person in people.find({'First Name': 'Lars'}):
    print(person)

But if I want to find all records of people whom have father's whose name is Gunnar, what would I write? 但是,如果我想查找父亲名字叫Gunnar的父亲的所有记录,我该怎么写?

I tried: 我试过了:

for person in people.find({'Father': {'First Name': 'Gunnar'}}):
    print(person)

But it returns no results. 但是它没有返回结果。

Use the dot notation : 使用点符号

for person in people.find({'Father.First Name': 'Gunnar'}):
    print(person)

See also: 也可以看看:

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

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