【问题标题】:How to create mongodb collection on runtime while executing Yii2 code?执行 Yii2 代码时如何在运行时创建 mongodb 集合?
【发布时间】:2022-08-23 20:52:49
【问题描述】:

我需要在执行代码时通过检查集合是否存在来创建 mongodb 集合。

我试过下面的东西

Yii::$app->db->createCommand()-> createCollection(\"collection_name\");

但未创建集合。

请帮忙。

    标签: php mongodb yii2 yii2-basic-app


    【解决方案1】:

    问题解决了..

    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',
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多