Mongodb – shard key specification

mongodbsharding

I have a document like so:

{
    "_id" : ObjectId("52f29f91ddbd75540aba6dae"),
    "teams" : [
        {
            "name" : "Washington Bullets",
            "score" : 100,
            "home" : false,
            "won" : 1,
            "players" : [
                {
                    "player" : "Jeff Ruland",
                    "pts" : 19
                },
                {
                    "player" : "Cliff Robinson",
                    "pts" : 22
                },
                {
                    "player" : "Gus Williams",
                    "pts" : 21
                },
                {
                    "player" : "Jeff Malone",
                    "pts" : 18
                },
                {
                    "player" : "Charles Jones",
                    "pts" : 4
                },
                {
                    "player" : "Dan Roundfield",
                    "pts" : 2
                },
                {
                    "player" : "Perry Moss",
                    "pts" : 7
                },
                {
                    "player" : "Dudley Bradley",
                    "pts" : 0
                },
                {
                    "player" : "Darren Daye",
                    "pts" : 5
                },
                {
                    "player" : "Tom McMillen",
                    "pts" : 2
                },
                {
                    "player" : "Manute Bol",
                    "pts" : 0
                }
            ]
        },
        {
            "name" : "Atlanta Hawks",
            "score" : 91,
            "home" : true,
            "won" : 0,
            "players" : [
                {
                    "player" : "Dominique Wilkins",
                    "pts" : 32
                },
                {
                    "player" : "Tree Rollins",
                    "pts" : 9
                },
                {
                    "player" : "Cliff Levingston",
                    "pts" : 8
                },
                {
                    "player" : "Spud Webb",
                    "pts" : 12
                },
                {
                    "player" : "Kevin Willis",
                    "pts" : 13
                },
                {
                    "player" : "Randy Wittman",
                    "pts" : 6
                },
                {
                    "player" : "Scott Hastings",
                    "pts" : 4
                },
                {
                    "player" : "Lorenzo Charles",
                    "pts" : 4
                },
                {
                    "player" : "Sedric Toney",
                    "pts" : 0
                },
                {
                    "player" : "Ray Williams",
                    "pts" : 0
                },
                {
                    "player" : "Jon Koncak",
                    "pts" : 3
                }
            ]
        }
    ]
}

I want to shard by teams.name. I tried doing:

sh.shardCollection("finalNBA.list", {teams:{name:1}})

But I get the error that the shard key can only be 1 or hashed. Does anyone know how to to enable sharding using an embedded field?

Best Answer

Try using, The key here is dot notation.

sh.shardCollection("finalNBA.list", {"teams.name":1}})

Make sure you have already created an index for test.name field.