Proper implementation of a hashed shard key in MongoDB

I have a collection that is currently indexed / requested by the built-in "_id" (ObjectId). I do not want to spoof this key, because it is sequential (with a date prefix). The documentation for Mongo 2.4 says that I can outline the hash of this key, which sounds great. For instance:

sh.shardCollection ("records.active", {_id: "hashed"})

Question: should I first create a hashed index in the active collection with:

db.active.ensureIndex ({_ id: "hashed"})

Or is it not necessary? I do not want to waste space with more indexing than necessary.

Related question: if I create a hashed index with makeIndex ({_id: "hashed"}), can I remove the default id index? Will Mongo help accept requests in the _id field, hash them and run them against the hashed index?

Thank...

+5
source share
2 answers

You will need the _id index and the _id hash index . In MongoDB 2.4, you do not need to explicitly call db.active.ensureIndex ({_ id: "hashed"}) before outlining your collection, but if you are not using sh.shardCollection ("records.active", {_id: "hashed" }) will create a hashed index for you.

Replication requires _id .

MongoDB, . MongoDB 2.4, hashed _id.

+3

, mongoDB 2.4.11.

. mongos. 1 000 000 , , shard A ( sh.status()).

, shard, ,

sh.shardCollection("database.collection",{_id:"hashed"})

,

{
    "proposedKey" : {
        "_id" : "hashed"
    },
    "curIndexes" : [
        {
            "v" : 1,
            "name" : "_id_",
            "key" : {
                "_id" : 1
            },
            "ns" : "database.collection"
        }
    ],
    "ok" : 0,
    "errmsg" : "please create an index that starts with the shard key before sharding."
}

,

  • ,
  • , MongoDB , , :

    db.collection.ensureIndex({_id: "hashed" })

0
source

All Articles