Sergio Porres
Sergio Porres

Reputation: 67

Sharding a Mongo collection with a compound index

I have a mongo collection that I need to shard. I am having a hard time understanding the docs; I have thought of doing this in two ways.

My doc:

public class MyShardedDocument
{
        public int DocID;
        public int AnotherIDThatMatters;
}

Approach 1:

  1. Add a property called ShardKey = DocID.ToString() + AnotherIDThatMatters.ToString()
  2. Add an index on ShardKey
  3. sh.shardCollection( "myDB.myCollection", { "ShardKey ": 1 })

Approach 2:

  1. Add a compound index on DocID, AnotherIDThatMatters
  2. sh.shardCollection( "myDB.myCollection", { "DocID": 1,"AnotherIDThatMatters": 1 })

Are both of these valid?

Upvotes: 2

Views: 305

Answers (1)

profesor79
profesor79

Reputation: 9473

  1. this solution relays on c# code execution, and if any document will be added to collection by other input than your application, shard key data will fail.

  2. this approach is universal, relays on data in the document (no external processing) and every inserted document is covered regardless of how it was inserted.

Upvotes: 1

Related Questions