简体   繁体   English

Mongoengine如何在集合中的EmbeddedDocumentListField中检索过滤的子文档

[英]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.

相关问题 我如何附加到 MongoEngine Flask 中的 EmbeddedDocumentListField? - How do i append to EmbeddedDocumentListField in MongoEngine Flask? 如何在 Mongoengine 中对 EmbeddedDocumentListField 对象进行分页? - How can I paginate EmbeddedDocumentListField objects in Mongoengine? 在mongoengine EmbeddedDocumentListField中按分组 - Group By in mongoengine EmbeddedDocumentListField mongoengine:如何查询EmbeddedDocumentListField中的非ascii StringField - mongoengine: how to query on a non-ascii StringField in EmbeddedDocumentListField Mongoengine-JSONify EmbeddedDocumentListField - Mongoengine - JSONify EmbeddedDocumentListField python mongoengine EmbeddedDocumentListField 和聚合 - python mongoengine EmbeddedDocumentListField and aggregate 如何在 mongoengine 中指定集合? - how to specify the collection in mongoengine? MongoEngine:EmbeddedDocumentListField()和ListField(EmbeddedDocumentField())之间的区别? - MongoEngine: difference between EmbeddedDocumentListField() and ListField(EmbeddedDocumentField())? 如何对 MongoDB 集合中的子文档进行分页? - How to paginate subdocuments in a MongoDB collection? 如何在另一个集合中创建文档时引用集合中的现有文档 MongoEngine - How to reference existing documents in collection on creation of documents in another collection MongoEngine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM