php - Retrieving an embedded document -


If I have a range that embeds many sub-class documents ... is there any sub- The best way to get a category is based on example ID).

This document does not work using a repository. I know that this is the current limit of Mongodybi and I have already voted.

So I'm wondering what is the best way to recover sub-categories? My current point of view looks like this:

  & lt ;? Php $ category = $ dm- & gt; CreateQueryBuilder ('category') - & gt; Field ('subCategories._id', new \ MongoId ($ Id)) - & gt; GetQuery () - & gt; GetSingleResult (); Foreign Currency ($ category-> getSubCategories () as the $ sub) {if ($ sub-> getId () === $ id) {$ subCategory = $ sub; break; So the nature of Mongodbi is that you can not return "sub-documents", because when a sub-category is recovered independently of your parents' documents, then you have to pull back the parent category and then To find a sub-object, use the loop.  

This type of nesting in MongoDB is more common because you are getting the entire document, you have to be ready to "drill down" a bit. Usually it involves writing some small helpful methods like you wrote.

The code you have submitted looks perfectly appropriate. In PHP, I strongly suggest wrapping this method on a suitable object category :: GetSubCategory ($ id) .

Comments