Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine MongoDB query embedded documents

I have top-level document User with embedded Keyword documents (User EmbedMany Keyword). Well i don't know how to get Keyword object from concrete User by Keyword's id I spent a lot of time trying to solve this but it still open problem for me. So i help someone here will help me. Thanks a lot.

like image 395
VitalyP Avatar asked Dec 21 '25 05:12

VitalyP


1 Answers

You can load only parent document from the mongodb. But there is $slice operator that can load parent document just with limited number of embedded documents.

On native mongodb language you can do it so:

db.users.find({_id: "UserId", "Keywords._id", "keywordId"},
               {Keywords:{$slice: 1}}) // first matched by id keyword

From doctrine you will receive user document with only one embedded keyword.

Hope this helps.

like image 149
Andrew Orsich Avatar answered Dec 23 '25 22:12

Andrew Orsich