I have a collection that I've been trying to shard, but I can't get it to shard with the index that I gave it.

The collection in question uses a hashed index, which I created using:

db.bar.ensureIndex({"_id": "hashed"}, {"background": 1})

But when I try to shard said collection, using:

sh.shardCollection("foo.bar", {"_id": "hashed"})

I get an error saying:

command failed: { "ok" : 0, "errmsg" : "shard keys must all be ascending" }

Does this mean that the data I have in collection bar needs to be ascending or that the version of Mongo that I'm using only supports range shard-keys?

  • What version of MongoDB are you using? Dec 3, 2013 at 21:53
  • @ranman version 2.0.4 Dec 4, 2013 at 15:26
  • 1
    Ah, unfortunately version 2.0.4 doesn't have support for hashed indexes as far as I know. Can you upgrade to 2.4? I'm surprised you don't get a different error message when ensuring the index type. Dec 4, 2013 at 18:00

1 Answer 1


Updating to the latest version of mongodb available for ubuntu fixed the issue. This time, running db.bar.ensureIndex({"_id": "hashed"}, {"background": 1}), gave me:

Inserted 1 record(s) in 97564ms

and when I ran sh.shardCollection("foo.bar", {"_id": "hashed"}), I got:

  "collectionsharded": "foo.bar",
  "ok": 1

Here's the bash script that I used to update the servers:


# for installing the latest and greatest version of MongoDB on Ubuntu Servers
if [[ "$UID" != 0 ]]; then # if not root, then exit non-zero
  printf "$(basename $0): err: %s\n" "must be root"
  exit 1

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 # import the MonboDB public GPG key
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list # create `/etc/apt/sources.list.d/mongodb.list` containing the ubuntu repo for mongodb.  
apt-get update # refresh the apt repos

for each in mongodb-clients mongodb-dev mongodb-server mongodb; do # remove the old version of mongodb
  apt-get remove "$each"

apt-get install mongodb-10gen # install mongodb from the newly added repo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.