我对Mongo分片键有疑问。我有以下列方式构建的文档:
{
"payload": {
"id": "364e1f2c-6d4c-45fb-af19-841149286d67",
"name": "John",
},
"source": "myApp",
"version": "1.0",
"additionalInfo": {
"time": "2012-04-18T17:32:11+03:00"
}
}
我希望我的分片键为:payload.name和additionalInfo.time。以下命令因语法错误而失败:
db.runCommand({ shardcollection : "collection.table", key : {additionalInfo.time: 1, payload.name: 1}})
是否可以创建这样的分片键,或者只接受顶级键。另外,如果我尝试插入没有分片键字段的文档,插入是否会失败?
答案 0 :(得分:5)
是的,您需要用引号括起您的关键字段:
db.runCommand({ shardcollection : "collection.table", key : {'additionalInfo.time': 1, 'payload.name': 1}})