如何使用pymongo

时间:2019-01-04 17:52:49

标签: python mongodb python-2.7 pymongo

我试图在使用pymongo之前在pymongo中检查mongodb中的子属性是否存在,因为通常情况下,如果我打印不存在的属性,程序将输出错误。

我可以做类似的事情来检查更高级别的属性是否存在

for x in raw200k.find({},{attrib1}):
    if 'attrib1' in x:
        print x[attrib1]
    else
        print 'does not exist.'

但是,此attrib1内部具有子属性。像这样:

attrib1:{
    subattrib1: "value"
}

我想要这样的东西:

for x in raw200k.find({},{attrib1.subattrib1}):
    if 'attrib1.subattrib1' in x:
        print x[attrib1.subattrib1]
    else
        print 'does not exist.'

尽管上面的代码没有错误,但是即使存在subattrib1,它也不会打印任何内容。

0 个答案:

没有答案