Imaging we have documents like this:
{
_id: ObjectId(""),
accountId: ObjectId(""),
userId: ObjectId(""),
someOtherFieldA: ["some", "array", "values"],
someOtherFieldB: ["other", "array", "values"],
...
}
Furthermore there are multiple compound indices, ex.:
{ userId: 1, someOtherFieldA: 1, ... }
{ userId: 1, someOtherFieldB: 1, ... }
We want to shard by accountId
.
Would it be enough to add a single field index for accountId
, so that the existing indices still work? Or would all indices need the accountId
as prefix (first part)?
When you run the sh.shardCollection()
command then MongoDB automatically creates an index on the shard key field (unless such an index exist already), so you don't need to care about this question.