Puede extender MappingMongoEntityInformation para anular getCollectionName(). Las operaciones del repositorio llaman a getCollectionName() en cada operación. Supongo que el identificador de inquilino sería un ThreadLocal
public class TenantThreadLocal extends ThreadLocal<String> {
private final static TenantThreadLocal instance = new TenantThreadLocal();
public static TenantThreadLocal instance() {
return instance;
}
}
Y la clase anulada con los constructores omitidos.
public class TenantMappingMongoEntityInformation<T, ID extends java.io.Serializable> extends MappingMongoEntityInformation<T, ID> {
@Override
public String getCollectionName() {
return TenantThreadLocal.instance().get() + super.getCollectionName();
}
}
Luego crea tu repositorio:
MongoPersistentEntity<YourObject> persistentEntity =
(MongoPersistentEntity<YourObject>)
mongoOperations.getConverter()
.getMappingContext()
.getPersistentEntity(YourObject.class);
MongoEntityInformation<YourObject, ObjectId> mongoEntityInformation =
new MappingMongoEntityInformation<YourObject, ObjectId>(persistentEntity);
CrudRepository<YourObject, ObjectId> repository =
new SimpleMongoRepository<YourObject, ObjectId>(mongoEntityInformation, mongoOperations);