Hi,
I have two collections. The structure is as below
Products
_id
name
description
companyId
Companies
_id
companyname
I am using manual references to link each product document to company.
How do i do a query such that when i find all products, i can get the info from company as well. In relational dataase, we do this via a join.
Here is a working version of what i come out with, however i think it can be improved. In my code, i am calling Companies::findbyid in every loop.
$products = Products::find(array(
"limit" => 5,
"sort" => array("_id" => -1)
));
foreach($promotions as $key => $value){
$company = Companies::findById($value->companyId);
$product[$key]->name = $value->name;
$product[$key]->description = $value->description;
$product[$key]->companyName = $company->name;
}
$this->view->products = $product;
Advice appreciate greatly!!