通过MongoEngine使用或操作查询一个EmbeddedDocumentFieldList
Query an EmbeddedDocumentFieldList by MongoEngine use or operate
我正在尝试查询 EmbeddedDocumentFieldList 字段中的内容,我想使用或操作条件。
架构如爆炸:
class EmbeddedDoc(EmbeddedDocument):
name = StringField()
class ParentDoc(Document):
name = StringField():
children = EmbeddedDocumentFieldList(EmbeddedDoc)
然后我想得到一些名为 "a" 或 "b" 的 ParentDoc 子项,我尝试使用 Q 函数,但程序抛出异常说 "filter method taken 1 params but it were given 2"。
还有其他办法吗?
谢谢
我不知道您的 "Q" 函数在做什么,但我将向您展示如何查询嵌入式文档。 (已编辑:我明白你现在在谈论 "Q"。添加到我下面的回答中)
看你的模型~
class EmbeddedDoc(EmbeddedDocument):
name = StringField()
def to_json(self):
return {
"name": self.name
}
class ParentDoc(Document):
name = StringField()
children = EmbeddedDocumentFieldList(EmbeddedDoc)
def to_json(self):
return {
"_id": str(self.pk),
"name": self.name,
"children": [child.to_json() for child in self.children]
}
我覆盖了 to_json() 方法以使其更易于阅读。那部分是可选的,但我推荐它。
我会在我的方法中做这样的事情~
pdoc = ParentDoc.objects(pk=some_ID_variable)
if pdoc:
pdoc = pdoc.get(pk=some_ID_variable)
if pdoc.children.filter(name=some_name_variable):
child = pdoc.children.get(name=some_name_variable)
return child.to_json()
return {"message": "Child with that name not found"}
return {"message": "Parent with ID not found"}
这是怎么回事?首先,我查询 ParentDoc 是否存在。如果存在,则从查询集中获取 ParentDoc 对象。使用过滤器按名称查询对象的嵌入对象,因为嵌入对象没有主键。如果存在 return json 格式的嵌入对象。
根据 Mongoengine 用户指南的 advanced queries 部分,要使用或运算符,我需要使用或按位运算符“|”连同一个 Q class。
所以我要添加这个 ~
from mongoengine.queryset.visitor import Q
并将上面的过滤器更改为~
children = []
cdocs = pdoc.children.filter(Q(someParam=someValue) | Q(anotherParam=anotherValue))
children.append(cdoc.tojson() for cdoc in cdocs)
return {"Children": children}
虽然我没有用过这个。希望对你有用!
我正在尝试查询 EmbeddedDocumentFieldList 字段中的内容,我想使用或操作条件。 架构如爆炸:
class EmbeddedDoc(EmbeddedDocument):
name = StringField()
class ParentDoc(Document):
name = StringField():
children = EmbeddedDocumentFieldList(EmbeddedDoc)
然后我想得到一些名为 "a" 或 "b" 的 ParentDoc 子项,我尝试使用 Q 函数,但程序抛出异常说 "filter method taken 1 params but it were given 2"。 还有其他办法吗? 谢谢
我不知道您的 "Q" 函数在做什么,但我将向您展示如何查询嵌入式文档。 (已编辑:我明白你现在在谈论 "Q"。添加到我下面的回答中)
看你的模型~
class EmbeddedDoc(EmbeddedDocument):
name = StringField()
def to_json(self):
return {
"name": self.name
}
class ParentDoc(Document):
name = StringField()
children = EmbeddedDocumentFieldList(EmbeddedDoc)
def to_json(self):
return {
"_id": str(self.pk),
"name": self.name,
"children": [child.to_json() for child in self.children]
}
我覆盖了 to_json() 方法以使其更易于阅读。那部分是可选的,但我推荐它。 我会在我的方法中做这样的事情~
pdoc = ParentDoc.objects(pk=some_ID_variable)
if pdoc:
pdoc = pdoc.get(pk=some_ID_variable)
if pdoc.children.filter(name=some_name_variable):
child = pdoc.children.get(name=some_name_variable)
return child.to_json()
return {"message": "Child with that name not found"}
return {"message": "Parent with ID not found"}
这是怎么回事?首先,我查询 ParentDoc 是否存在。如果存在,则从查询集中获取 ParentDoc 对象。使用过滤器按名称查询对象的嵌入对象,因为嵌入对象没有主键。如果存在 return json 格式的嵌入对象。
根据 Mongoengine 用户指南的 advanced queries 部分,要使用或运算符,我需要使用或按位运算符“|”连同一个 Q class。 所以我要添加这个 ~
from mongoengine.queryset.visitor import Q
并将上面的过滤器更改为~
children = []
cdocs = pdoc.children.filter(Q(someParam=someValue) | Q(anotherParam=anotherValue))
children.append(cdoc.tojson() for cdoc in cdocs)
return {"Children": children}
虽然我没有用过这个。希望对你有用!