Adding custom indexes in sharded cluster CSRS(Config Server as replicaSet )?

Solution 1:

Your query covers all collections, however usually it should be per collection, i.e. per namespace. In earlier MongoDB release the documents where like this:

{ 
    "_id": ObjectId("61a7bd45b9a53380435dede7"), 
    "ns": "database.collection", 
    "min": ..., 
    "max": ...., 
    "shard": "shard_02", 
    ...
}

Now in MongoDB 5.0 field ns is replaced by uuid and it looks like this:

{ 
    "_id": ObjectId("61a7bd45b9a53380435dede7"), 
    "uuid": UUID("322758c1-c52f-4ab6-9eb2-c48fc1634ef7"), 
    "min": ..., 
    "max": ...., 
    "shard": "shard_02", 
    ...
}

You should have an unique index on {uuid: 1, shard: 1, min: 1}. You can lookup namespace from config.collections collection.

Perhaps prometheus/grafana does not take this modification into account.

The $group stage can sometimes use an index to find the first document in each group if all of the following criteria are met:

  • The $group stage is preceded by a $sort stage that sorts the field to group by,
  • There is an index on the grouped field which matches the sort order and
  • The only accumulator used in the $group stage is $first.

So, I don't think an index on {shard: 1} will help