public Optional<IndexFieldTypesDTO> upsert(IndexFieldTypesDTO dto) {
        final WriteResult<IndexFieldTypesDTO, ObjectId> update = MongoDBUpsertRetryer.run(() -> db.update(
                DBQuery.and(
                        DBQuery.is(FIELD_INDEX_NAME, dto.indexName()),
                        DBQuery.is(FIELD_INDEX_SET_ID, dto.indexSetId())
                ),
                dto,
                true,
                false
        ));

        final Object upsertedId = update.getUpsertedId();
        if (upsertedId instanceof ObjectId) {
            return get(((ObjectId) upsertedId).toHexString());
        } else if (upsertedId instanceof String) {
            return get((String) upsertedId);
        }
        return Optional.empty();
    }
@Test
    public void upsert() {
        final IndexFieldTypesDTO newDto1 = createDto("graylog_0", Collections.emptySet());
        final IndexFieldTypesDTO newDto2 = createDto("graylog_1", Collections.emptySet());

        assertThat(dbService.findAll().size()).isEqualTo(0);

        final IndexFieldTypesDTO upsertedDto1 = dbService.upsert(newDto1).orElse(null);
        final IndexFieldTypesDTO upsertedDto2 = dbService.upsert(newDto2).orElse(null);

        assertThat(upsertedDto1).isNotNull();
        assertThat(upsertedDto2).isNotNull();

        assertThat(upsertedDto1.indexName()).isEqualTo("graylog_0");
        assertThat(upsertedDto2.indexName()).isEqualTo("graylog_1");

        assertThat(dbService.findAll().size()).isEqualTo(2);

        assertThat(dbService.upsert(newDto1)).isNotPresent();
        assertThat(dbService.upsert(newDto2)).isNotPresent();

        assertThat(dbService.findAll().size()).isEqualTo(2);
    }