mongodb可以直接在mongodb数据库里面进行直接创建索引,也可以在fecmall代码里面创建,首先,先看一下,fecmall的mongodb的表的索引是怎么创建的
1.migrate部分:https://github.com/fecshop/yii2_fecshop/blob/master/migrations/mongodb/m170228_072455_fecshop_tables.php
<?php
class m170228_072455_fecshop_tables extends \yii\mongodb\Migration
{
public function up()
{
\fecshop\models\mongodb\Product::create_index();
\fecshop\models\mongodb\cms\Article::create_index();
\fecshop\models\mongodb\cms\StaticBlock::create_index();
\fecshop\models\mongodb\customer\Newsletter::create_index();
\fecshop\models\mongodb\product\Favorite::create_index();
\fecshop\models\mongodb\product\Review::create_index();
\fecshop\models\mongodb\url\UrlRewrite::create_index();
}
public function down()
{
echo "m170228_072455_fecshop_tables cannot be reverted.\n";
return false;
}
}
以\fecshop\models\mongodb\Product::create_index();
为例子
打开这个文件:https://github.com/fecshop/yii2_fecshop/blob/master/models/mongodb/Product.php#L130
/**
* 给model对应的表创建索引的方法
* 在indexs数组中填写索引,如果有多个索引,可以填写多行
* 在migrate的时候会运行创建索引,譬如:
* @fecshop/migrations/mongodb/m170228_072455_fecshop_tables
*/
public static function create_index()
{
$indexs = [
['spu' => -1],
['sku' => -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];
foreach ($indexs as $columns) {
self::getCollection()->createIndex($columns, $options);
}
}
您可以照葫芦画瓢自己写一下