Mongodb query to retrieve data from each category

I work with mongodb-odm, I have a database of videos distributed according to categories such as humor, opinion, joke, etc.

For the homepage, I need at least 5 videos from each category. Is it possible to build a query to receive 5 documents from each category?

What I tried is to separate the queries one by one for each category, which doesn't seem like a good idea.

public function fetchTopVideos(){ $dm = parent::getDocumentManager(); $res = $this->app->make('stdClass'); $res->status = 0; $limit = 5; $videos1 = $dm->getRepository("Videos")->findBy(array("category"=>"humor"),array(),$limit,0); $videos2 = $dm->getRepository("Videos")->findBy(array("category"=>"opinion"),array(),$limit,0); }; ...... ...... 

My Mongo Document looks something like the code below, except that the actual document has many other fields.

 { "_id" : ObjectId("55c0866cbff0fa431d8b4567"), "title" : "Harsha Bhogle at IIM", "alias" : "harsha-bhogle-at-iim", "url" : "https://www.youtube.com/embed/kaw_bKOkULM?autoplay=0&rel=0&showinfo=0&modestbranding=1", "category" : "opinion", "tags":[ "cricket", "IIM" ], "thumbnailUrl":"http://i.ytimg.com/vi/kaw_bKOkULM/hqdefault.jpg", "logtime":"1449663691" } 

Even a logic proposal is appreciated !!

+5
source share

Source: https://habr.com/ru/post/1237545/


All Articles