通过子类字段查询MongoDB

时间:2020-08-07 07:53:20

标签: python mongodb mongoengine

已解决:

这有效:

print("\nTrying to access by embedded_doc__embedded_int=1:")
for data in Doc.objects(embedded_doc__embedded_int=1):
    print(data)

您必须使用主类变量名(而不是类名),后跟__和子类变量名来访问子类字段,如上所述。

更新:

我的原始问题如下。我写了一个示例,以简明扼要的形式显示我要做什么。

在此示例中,我有一个Doc类。每个文档都有一个称为“ embedded_doc”的嵌入式类。在Embedded类中,有一个称为“ embedded_int”的整数。

我的目标是通过MongoEngine将文档存储在MongoDB中,并在数据库中查询具有Embedded_doc.embedded_int == 1的文档。

到目前为止,我还无法弄清楚该怎么做。

class Embedded(EmbeddedDocument):
    embedded_int = IntField()
    
    def __eq__(self, other):
        return other == self.embedded_int
    
    def __str__(self):
        return(str(self.embedded_int))
    
class Doc(Document):
    doc_str = StringField()
    embedded_doc = EmbeddedDocumentField(Embedded)
    
    def __str__(self):
        return f"{self.doc_str} {str(self.embedded_doc)}"

data1 = Doc(doc_str = "first", embedded_doc = Embedded(embedded_int = 1))
data2 = Doc(doc_str = "second", embedded_doc = Embedded(embedded_int = 2))

#Gives correct output:
#Showing Doc objects in database:
#first 1
#second 2
print("Showing Doc objects in database:")
for data in Doc.objects():
    print(data)

#Gives correct output:
#Trying to access by doc_str='first':
#first 1
print("\nTrying to access by doc_str='first':")
for data in Doc.objects(doc_str='first'):
    print(data)

#ValueError: The source SON object needs to be of type 'dict' but a '<class 'int'>' was found
#During handling of the above exception, another exception occurred:
#mongoengine.errors.InvalidQueryError: Querying the embedded document 'Embedded' failed, due to an invalid query value
#print("\nTrying to access by embedded_doc=1:")
#for data in Doc.objects(embedded_doc=1): 
#    print(data)


#SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
#print("\nTrying to access by embedded_doc.embedded_int=1:")
#for data in Doc.objects(embedded_doc.embedded_int=1): 
#    print(data)


#NameError: name 'embedded_doc' is not defined
#print("\nTrying to access by embedded_doc.embedded_int==1:")
#for data in Doc.objects(embedded_doc.embedded_int==1): 
#    print(data)


#SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
#print("\nTrying to access by Embedded.embedded_int=1:")
#for data in Doc.objects(Embedded.embedded_int=1): 
#    print(data)


#Runs, but gives incorrect output:
#Trying to access by Embedded.embedded_int==1:
#first 1
#second 2
print("\nTrying to access by Embedded.embedded_int==1:")
for data in Doc.objects(Embedded.embedded_int==1): 
    print(data)

原始问题:

我正在使用Python + MongoDB + MongoEngine来开始使用NoSQL数据库。

我有一个类Article,其中包含字段ArticleMetadata。反过来,ArticleMetadata包含一个名为pub_year的字段。我想在数据库中查询包含pub_year == 2002的ArticleMetadata的文章。我正在尝试这样做:

for article in Article.objects(ArticleMetadata.pub_year == 2002):
    print(article)
    input()

但是它正在打印数据库中的每篇文章,而不仅仅是pub_year == 2002的文章。我需要更改什么?

1 个答案:

答案 0 :(得分:0)

尝试使用ArticleMetadata__pub_year = 2002而不是ArticleMetadata.pub_year == 2002