PHP7 MongoDB - 创建索引

时间:2017-10-18 21:23:57

标签: php mongodb

我正在移动一个在mongo中创建索引的php脚本,从php5到php7。

我尝试使用以下MongoDB \ Driver \ Command但它返回此错误。

任何人都知道如何为现有集合创建索引?

$index = ['id' => 1, 'user' => 1, 'time' => 1];
$cmd = new MongoDB\Driver\Command([
     'createIndexes' => $collection_name, 
     'indexes' => $index
]);
$mongo_client->executeCommand($mongo_database, $cmd);

PHP致命错误:未捕获的MongoDB \ Driver \ Exception \ RuntimeException:没有这样的cmd:createIndexes

1 个答案:

答案 0 :(得分:0)

我不确定你是否已经解决了,但我找到了解决方案:

 $manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
 $command = new MongoDB\Driver\Command([
    "createIndexes" => "collectionName",
    "indexes"       => [[
      "name" => "indexName",
      "key"  => [ "keyName" => 1],
      "ns"   => "databaseName.collectionName",
   ]],
]);
$result = $manager->executeCommand("databaseName", $command);

致记:https://github.com/mongodb/mongo-php-driver/issues/170