2015-07-01 34 views
0

一個分片鑰匙我的MongoDB是一個新手,這裏是一個簡單的例子,但得到了一個錯誤:不能使用索引鍵(_id)在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_的指數,爲什麼錯誤仍時有發生?

回答

0

我從手冊中找到了答案。

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