【发布时间】:2022-08-23 20:52:49
【问题描述】:
我需要在执行代码时通过检查集合是否存在来创建 mongodb 集合。
我试过下面的东西
Yii::$app->db->createCommand()-> createCollection(\"collection_name\");
但未创建集合。
请帮忙。
标签: php mongodb yii2 yii2-basic-app
我需要在执行代码时通过检查集合是否存在来创建 mongodb 集合。
我试过下面的东西
Yii::$app->db->createCommand()-> createCollection(\"collection_name\");
但未创建集合。
请帮忙。
标签: php mongodb yii2 yii2-basic-app
问题解决了..
Yii::$app->db->createCommand()->createCollection("collection_name")->execute();
并且索引将添加为收藏,
$collectionName = Yii::$app->db->getCollection("collection_name");
$collectionName->createIndexes([
'key' => ['id' => 'int'],
'name' => 'id_index'
],
[
'key' => ['id' => 'int', 'category' => 'text'],
'name' => 'id_category_index',
]);
【讨论】: