@Override
    public EntitySuggestionResponse suggest(final String collection,
                                            final String valueColumn,
                                            final String query,
                                            final int page,
                                            final int perPage,
                                            final Subject subject) {
        final MongoCollection<Document> mongoCollection = mongoConnection.getMongoDatabase().getCollection(collection);

        final var bsonFilter = !Strings.isNullOrEmpty(query)
                ? Filters.regex(valueColumn, query, "i")
                : Filters.empty();

        final var resultWithoutPagination = mongoCollection
                .find(bsonFilter)
                .projection(Projections.include(valueColumn))
                .sort(Sorts.ascending(valueColumn));

        final var userCanReadAllEntities = permissionsUtils.hasAllPermission(subject) || permissionsUtils.hasReadPermissionForWholeCollection(subject, collection);
        final var skip = (page - 1) * perPage;
        final var checkPermission = permissionsUtils.createPermissionCheck(subject, collection);
        final var documents = userCanReadAllEntities
                ? mongoPaginate(resultWithoutPagination, perPage, skip)
                : paginateWithPermissionCheck(resultWithoutPagination, perPage, skip, checkPermission);

        final List<EntitySuggestion> suggestions = documents
                .map(doc ->
                        new EntitySuggestion(
                                doc.getObjectId(ID_FIELD).toString(),
                                doc.getString(valueColumn)
                        )
                )
                .toList();

        final long total = userCanReadAllEntities
                ? mongoCollection.countDocuments(bsonFilter)
                : MongoUtils.stream(mongoCollection.find(bsonFilter).projection(Projections.include(ID_FIELD))).filter(checkPermission).count();

        return new EntitySuggestionResponse(suggestions,
                PaginatedList.PaginationInfo.create((int) total,
                        suggestions.size(),
                        page,
                        perPage));
    }
@Test
    void checksPermissionsForEachDocumentWhenUserDoesNotHavePermissionForWholeCollection() {
        doReturn(false).when(entityPermissionsUtils).hasAllPermission(subject);
        doReturn(false).when(entityPermissionsUtils).hasReadPermissionForWholeCollection(subject, "dashboards");

        final Collection<String> permittedIds = List.of("5a82f5974b900a7a97caa1e5", "5a82f5974b900a7a97caa1e7");
        doReturn((Predicate<Document>) document -> permittedIds.contains(document.getObjectId(EntityPermissionsUtils.ID_FIELD).toString()))
                .when(entityPermissionsUtils)
                .createPermissionCheck(subject, "dashboards");

        final var result = toTest.suggest("dashboards", "title", "", 1, 10, subject);

        assertThat(result.pagination().count()).isEqualTo(2);

        final var suggestions = result.suggestions();
        assertThat(suggestions).hasSize(2);
        assertThat(suggestions.stream().map(EntitySuggestion::id).toList())
                .containsExactlyInAnyOrder("5a82f5974b900a7a97caa1e5", "5a82f5974b900a7a97caa1e7");
        assertThat(suggestions.stream().map(EntitySuggestion::value).toList())
                .containsExactlyInAnyOrder("Test", "Test 3");
    }