Noam Malter
Noam Malter

Reputation: 83

Mongo DB Nested Shard Key

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?

Upvotes: 5

Views: 1194

Answers (1)

Remon van Vliet
Remon van Vliet

Reputation: 18625

It is, you need to enclose your key fields with quotes :

db.runCommand({ shardcollection : "collection.table", key : {'additionalInfo.time': 1, 'payload.name': 1}})

Upvotes: 6

Related Questions