简体   繁体   中英

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 . The Order is an embedded array in the Customer collection. Inside one Order there is an @ReferencedMany to the Product collection.

I now want to do some reporting on every Product that a Customer has ever bought.

What would be the MongoDB way to find all the products for this customer and add filters on this query. I don't want to loop every order and then fetch all the products and put them in a Doctrine ArrayCollection.

Is there a direct way to query these products?

Mongo DB does not fetch references automatically in single query. 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. But as consequence the queries on Product will become more complex.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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