PHP中的MongoDB createIndex
我在MongoDB中存储我的文章视图计数,如下所示PHP中的MongoDB createIndex,php,mongodb,Php,Mongodb,我在MongoDB中存储我的文章视图计数,如下所示 $mongoCollection = $this->mongoClient->db->collectionName; $mongoCollection->findAndModify( [ 'id' => (int)$article_id ], [ '$inc' => [
$mongoCollection = $this->mongoClient->db->collectionName;
$mongoCollection->findAndModify(
[
'id' => (int)$article_id
],
[
'$inc' => [
'count' => 1
]
],
null,
[
'upsert' => true
]
);
现在我需要添加一个索引,所以我只是添加
$mongoCollection->createIndex(['id' => 1]);
紧接着
$mongoCollection = $this->mongoClient->db->collectionName;
但这给了我
名为:id_1的索引已存在,并具有不同的选项
但是为什么呢?到那时,它必须工作吗
我做错了什么?这是正确的补充吗
['unique' => true, 'dropDups' => 1]
在这种情况下?错误消息说明了一切:名为:id_1的索引已经存在,并且有不同的选项
你们都准备好了一个同名的索引。您只需要创建一次索引,而不是每次连接到数据库时。似乎是合法的,所以我知道我只需要在某个地方创建一次,然后让它运行是的,只创建一次。我只需要使用Mongo shell,而不用麻烦将其放入自己的代码中