[英]Mongoengine how to retrieve filtered subdocuments in an EmbeddedDocumentListField in a collection
I have a document model as follows:我有一个文件 model 如下:
class MyDocumentModel(DynamicDocument):
subdocuments = EmbeddedDocumentListField(MySubdocumentModel)
class MySubdocumentModel(EmbeddedDocument):
some_field = StringField()
What I want to be able to do is get a list of all MySubdocumentModel
embedded documents contained in MyDocumentModel
documents in the entire collection by filtering subdocuments
on some_field
.我想要做的是通过过滤
subdocuments
上的子文档来some_field
整个集合中MyDocumentModel
文档中包含的所有MySubdocumentModel
嵌入文档的列表。 Does anyone know if this is possible/how to accomplish this in mongoengine
?有谁知道这是否可能/如何在
mongoengine
中完成? My research up to this point has not yielded anything that has worked.到目前为止,我的研究还没有产生任何有效的东西。
Things I've tried:我尝试过的事情:
MyDocumentModel.objects(subdocuments__some_field="my_field_value")
MyDocumentModel.objects(subdocuments__match={"some_field": "my_field_value"})
Any advice would be greatly appreciated.任何建议将不胜感激。
With some additional searching and experimenting, I found you can do this:通过一些额外的搜索和试验,我发现你可以这样做:
subdocs = MyDocumentModel._get_collection().aggregate([
{"$unwind": "$subdocuments"},
{"$match": {'subdocuments.some_field':'my_field_value'}}
])
I hope this helps others who need to do something similar.我希望这可以帮助其他需要做类似事情的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.