dbdao吧 关注:1,183贴子:4,074
  • 1回复贴,共1

我测试sharding时提示出错如上,不过我是给id加过索引呀。

只看楼主收藏回复

我测试sharding时提示出错如上,不过我是给id加过索引呀。
for(var i=1;i<=10;i++) db.users_hash.insert({_id:i,name:"tao"+i,addr:"BeiJing"});
db.collection.createIndex( { _id: "hashed" } )
db.users_hash.getIndexKeys()
sh.shardCollection("testshard.users_hash", { "_id": "hashed" } )
北京-梦里香 2015/12/23 14:25:03
mongos> sh.shardCollection("testshard.users_hash", { "_id": "hashed" } )
{
"proposedKey" : {
"_id" : "hashed"
},
"curIndexes" : [
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "testshard.users_hash"
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."


1楼2015-12-23 14:30回复
    odm finding:
    am a newbie of mongodb,here is a simple case but got an error:
    I want to use _id field (generated and indexed automatically) as a sharding key in mongodb, but got this error.
    {"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."
    }
    the error message showed that I already have an Index named _id_ on key _id, why the error still occur?
    0down voteaccepted I found an answer from manual.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.And here is a solution:fromEnforce Unique Keys for Sharded Collections
    http://stackoverflow.com/questions/31158499/can-not-use-indexed-key-id-as-a-sharding-key-in-mongodb


    2楼2015-12-23 14:30
    回复