Implement your entity classes from DatabaseShardedEntity like the example given below. This will ensure the writes to the entity gets routed to the right database.
Autowire the databaseShardedMongoTemplate and use it wherever required.
Sharding Hint
In order to route the write queries, the entities are supposed to implement from DatabaseShardedEntity. But, the find queries can take place with different criterion, with different fields. In order to route the find query to the right database, sharding hint is used.
If the sharding hint is not set, methods will throw a UnresolvableShardException.
Using Hint Resolution Callback
Refer to the Collection Sharding Strategy document to know how to use HintResolutionCallback for resolving the Sharding hint by registering callbacks to the MongoTemplate object.