I have a question regarding Mongo shard keys. I have documents which are structured in the following way:
{
"payload": {
"id": "364e1f2c-6d4c-45fb-af19-841149286d67",
"name": "John",
},
"source": "myApp",
"version": "1.0",
"additionalInfo": {
"time": "2012-04-18T17:32:11+03:00"
}
}
I'd like my shard to key to be: payload.name and additionalInfo.time. The following command fails on syntax error:
db.runCommand({ shardcollection : "collection.table", key : {additionalInfo.time: 1, payload.name: 1}})
Is it possible to create such a shard key or only top level keys are acceptable. Additionally, if I try to insert a document which does not have the shard key fields, will the insert fail?
The “shard key” is used to distribute the MongoDB collection's documents across all the shards. The key consists of a single field or multiple fields in every document. The sharded key is immutable and cannot be changed after sharding. A sharded collection only contains a single shard key.
Although you can have a unique compound index where the shard key is a prefix, if using unique parameter, the collection must have a unique index that is on the shard key. You cannot specify a unique constraint on a hashed index.
For more details on the db. printShardingStatus() output, see the sharded collection section on the sh. status() page.
It is, you need to enclose your key fields with quotes :
db.runCommand({ shardcollection : "collection.table", key : {'additionalInfo.time': 1, 'payload.name': 1}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With