Skip to content

Commit

Permalink
分类页aggregate函数的优化以及索引的添加
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyecommerce committed Jul 29, 2017
1 parent f16a5c3 commit bbd1546
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion models/mongodb/Product.php
Expand Up @@ -111,7 +111,11 @@ public static function create_index()
$indexs = [
['spu' => -1],
['sku' => -1],
['category' => -1],
['category' => -1,'score' => 1],
['category' => -1,'review_count' => 1],
['category' => -1,'favorite_count' => 1],
['category' => -1,'created_at' => 1],
['category' => -1,'final_price' => 1],
];

$options = ['background' => true];
Expand Down
13 changes: 13 additions & 0 deletions services/product/ProductMongodb.php
Expand Up @@ -20,6 +20,7 @@
class ProductMongodb implements ProductInterface
{
public $numPerPage = 20;
public $maxCount = 6000;

protected $_productModelName = '\fecshop\models\mongodb\Product';
protected $_productModel;
Expand Down Expand Up @@ -541,6 +542,12 @@ public function getFrontCategoryProducts($filter)
$project[$column] = 1;
$group[$column] = ['$first' => '$'.$column];
}

$langCode = Yii::$service->store->currentLangCode;
$name_lang = Yii::$service->fecshoplang->getLangAttrName('name',$langCode);
$project['name'] = [
$name_lang => 1
];
$pipelines = [
[
'$match' => $where,
Expand All @@ -559,6 +566,9 @@ public function getFrontCategoryProducts($filter)
[
'$sort' => $orderBy,
],
[
'$limit' => $this->maxCount,
],
];
$product_data = $this->_productModel->getCollection()->aggregate($pipelines);
$product_total_count = count($product_data);
Expand Down Expand Up @@ -595,6 +605,9 @@ public function getFrontCategoryFilter($filter_attr, $where)
[
'$group' => $group,
],
[
'$limit' => $this->maxCount,
],
];
$filter_data = $this->_productModel->getCollection()->aggregate($pipelines);

Expand Down

0 comments on commit bbd1546

Please sign in to comment.