mongodb 中不能使用索引键 (_id) 作为分片键

Can not use indexed key(_id) as a sharding key in mongodb

我是mongodb的新手,这里有一个简单的案例但是报错了:

我想使用 _id 字段(自动生成和索引)作为 mongodb 中的分片键,但出现此错误。

{
"proposedKey" : {
    "_id" : "hashed"
},
"curIndexes" : [
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "jackfruit.scenicspots"
    },
    {
        "v" : 1,
        "key" : {
            "geoLocation" : "2dsphere"
        },
        "name" : "geoLocation_2dsphere",
        "ns" : "jackfruit.scenicspots",
        "2dsphereIndexVersion" : 2
    }
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."

}

错误信息显示我已经在键 _id 上有一个名为 _id_ 的索引,为什么仍然出现错误?

我从手册中找到了答案。

MongoDB does not support creating new unique indexes in sharded collections and will not allow you to shard collections with unique indexes on fields other than the _id field.

这是一个解决方案:

来自Enforce Unique Keys for Sharded Collections