简体   繁体   English

Doctrine2和MongoDB查询文档中引用文档的嵌入式数组的嵌入式数组

[英]Doctrine2 and MongoDB Query on embedded array of an embedded array of referenced documents in a Document

I have a collection of Customer that have multiple Order . 我有一个具有多个OrderCustomer集合。 The Order is an embedded array in the Customer collection. Order是“ Customer集合中的嵌入式数组。 Inside one Order there is an @ReferencedMany to the Product collection. 在一个Order中, Product集合有一个@ReferencedMany

I now want to do some reporting on every Product that a Customer has ever bought. 现在,我想对Customer购买的每种Product进行一些报告。

What would be the MongoDB way to find all the products for this customer and add filters on this query. MongoDB为该客户查找所有产品并在此查询上添加过滤器的方式是什么。 I don't want to loop every order and then fetch all the products and put them in a Doctrine ArrayCollection. 我不想循环每个订单,然后获取所有产品,然后将它们放入Doctrine ArrayCollection中。

Is there a direct way to query these products? 有直接查询这些产品的方法吗?

Mongo DB does not fetch references automatically in single query. Mongo DB不会在单个查询中自动获取引用。 Maybe some drivers support such functionality and may produce sub-query/ies transparent for user. 也许某些驱动程序支持此类功能,并且可能产生对用户透明的子查询。

You can aggregate the data required for report to one collection using map-reduce pattern, for example, or embed Product data to Customer. 例如,您可以使用map-reduce模式将报告所需的数据汇总到一个集合中,或将产品数据嵌入到客户中。 But as consequence the queries on Product will become more complex. 但是结果是,对Product的查询将变得更加复杂。

You can de-normalize data and duplicate some Product data required for reports in Customer collection. 您可以对数据进行非规范化并复制“客户”集合中的报表所需的某些“产品”数据。 Then you'll have to deal with synchronization of those collections. 然后,您将不得不处理这些集合的同步。

Working on same issue, I came to the conclusion that if you want to reflect relations between objects you should use relational database :) 在同一问题上,我得出的结论是,如果您想反映对象之间的关系 ,则应该使用关系数据库:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM