Reputation: 12
Let's say that I had a collection called "History" and "Employees" and there will a document on it.
When I create first collection with
sh.shardCollection("Databases.History",{"email":1})
it works fine, and I add some document on it.
But. When I create the second collections named "Employees" with
db.Employees.insert({"name":"Jhon Wick", "email":"[email protected]"})
and create a shard collections on it with
sh.shardCollection("Databases.Employees",{"email":1})
, it doesn't work.
MongoDB says that "please create an index that starts with the shard key before sharding."
so I tried add some index to Employees with db.Employees.ensureIndex({"email":"hashed"})
and it works. But when I tried shardCollection
again, it still return that error.
What should I do?
Do MongoDB have a rules that if I would add shardKeys on a collection, it (collection) must not exist at first?
Upvotes: 0
Views: 821
Reputation: 4413
Your index strategy must be same as yout shard strategy. If you have created a hashed index, then you must specify hashed strategy while sharding.
sh.shardCollection("Databases.Employees",{"email":1})
shoule be replace with
sh.shardCollection("Databases.Employees",{"email":"hashed"})
Upvotes: 2