balaphp
balaphp

Reputation: 1326

mongo db - get table records with count of another collection

I have 2 collections, category and product. Each product has a category_id. i want to list the category names with product count... I have done this with 1 looping and 2 queries. but i want make this as a single query. How to generate that in mongo DB?

public function GetCategories(){

    $data = array();

    $this->cimongo->where(array('status'=>'1'));
    $query = $this->cimongo->get('category');
    foreach($query->result_array() as $category){ 
        $data[] = array(
            'keyword'       => $category['keyword'], 
            'name'          => $category['name'],
            'total'         => $this->GetTotalWordsByCategoryId($category['_id'])
        );
    }
    return $data;
}

Upvotes: 0

Views: 369

Answers (1)

Salvador Dali
Salvador Dali

Reputation: 222541

There is absolutely nothing that can make this as a single query. Mongodb does not have joins, so and because of this if you want to make it as one query you have to store information about second collection in the first.

Upvotes: 1

Related Questions