我将我的文章视图计数存储在MongoDB中,如下
$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的索引已存在,具有不同的选项
但为什么呢?通过http://php.net/manual/en/mongocollection.createindex.php和https://docs.mongodb.org/v3.0/tutorial/create-an-index/它一定有效吗?
我做错了什么?添加正确吗
['unique' => true, 'dropDups' => 1]
在这种情况下?
错误消息说明了一切:名为id_1的索引已经存在,具有不同的选项
你们都准备好了一个同名的索引。您只需要创建一次索引,而不是每次连接到数据库时都创建。