Couldn’t find valid index for shard key

i have the same issue as Unable to shard collection - won't use specified index : “couldn’t find valid index for shard key”. But the index is created.

Any help?

Welcome to the MongoDB Community Forums @Damian_Corbalan !

Please provide some more details on your environment:

  • Specific version of MongoDB server

  • Commands/steps you are taking to shard the collection. For example, are you also creating a hashed index similar to the post you referenced?

Thanks,
Stennie

Hi. I am running MongoDB 5.0.1 Community edition.

i created the index in a collection and then i try to create de shardKey with the created index.

mongos> db.puertos.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "hostname" : 1
                },
                "name" : "hostname",
                "background" : false
        },
        {
                "v" : 2,
                "key" : {
                        "hostname" : 1,
                        "name" : 1
                },
                "name" : "hostname_1_name_1"
        },
        {
                "v" : 2,
                "key" : {
                        "hostname" : "hashed",
                        "name" : 1,
                        "type" : 1
                },
                "name" : "hostname_hashed_name_1_type_1"
        }
]

And when i run

sh.shardCollection("vision.puertos",  { hostname : 1,  name : 1 })
{
        "ok" : 0,
        "errmsg" : "couldn't find valid index for shard key",
        "code" : 72,
        "codeName" : "InvalidOptions",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1645474030, 11),
                "signature" : {
                        "hash" : BinData(0,"B5QaUTVu8bU38bGyVtOGv3D8+MA="),
                        "keyId" : NumberLong("7029902955700551682")
                }
        },
        "operationTime" : Timestamp(1645474030, 7)
}

Please provide the output of the following command while connected to mongos.

use vision
db.puertos.getIndexes()

here it is

mongos> use vision
switched to db vision
mongos> db.puertos.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "hostname" : 1
                },
                "name" : "hostname",
                "background" : false
        },
        {
                "v" : 2,
                "key" : {
                        "hostname" : 1,
                        "name" : 1
                },
                "name" : "hostname_1_name_1"
        },
        {
                "v" : 2,
                "key" : {
                        "hostname" : "hashed",
                        "name" : 1,
                        "type" : 1
                },
                "name" : "hostname_hashed_name_1_type_1"
        }
]
mongos>

I am not too sure but it looks like you have too many indexes with hostname as prefix.

The index {hostname:1} is certainly redundant and useless because you also have {hostname:1,name:1}. Retry after removing this index.

Is the last index (hostname,name,type) really used for some use-case? It should not be a problem but that’s at the edge of my knowledge.

You do not specify any options so it puzzles me that you get InvalidOptions. Try passing 2 empty objects (with ,{},{}) so see what is going on.

I am trying to delete the indexes using mongoDB Compass but i have this error:

cannot perform operation: an index build is currently running for collection with UUID: c9c396c9-60c6-42e0-8959-09a8e07c9ea0

Any help?

Ok, after a while the indexes were deleted. I left only the the index hostname_1_name_1 and the issue continue.

mongos> use vision
switched to db vision
mongos> db.puertos.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "hostname" : 1,
                        "name" : 1
                },
                "name" : "hostname_1_name_1"
        }
]
mongos> sh.shardCollection("vision.puertos",  {hostname : 1,name : 1})
{
        "ok" : 0,
        "errmsg" : "couldn't find valid index for shard key",
        "code" : 72,
        "codeName" : "InvalidOptions",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1645531311, 12),
                "signature" : {
                        "hash" : BinData(0,"We0zeuM/UvJAoJOaqp/xoYJ7YNk="),
                        "keyId" : NumberLong("7029902955700551682")
                }
        },
        "operationTime" : Timestamp(1645531311, 8)
}
mongos> sh.shardCollection("vision.puertos",  {})
{
        "ok" : 0,
        "errmsg" : "Shard key is empty",
        "code" : 2,
        "codeName" : "BadValue",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1645531321, 6),
                "signature" : {
                        "hash" : BinData(0,"yf31C5g5e3DGv/XC1wdpuAmDsvM="),
                        "keyId" : NumberLong("7029902955700551682")
                }
        },
        "operationTime" : Timestamp(1645531311, 8)
}

Any clue?

May be you are hitting a BUG with hashed key index
Check this jira ticket.As per this ticket it is fixed in 4.9

https://jira.mongodb.org/browse/SERVER-48471

But, i am running 5.0.1 version