Prevent Spring Data for Mongo to convert ids to ObjectId

I finally found a solution for this. Probably not the best option, but works.

What I did was remove the converter from String to ObjectId that MongoTemplate uses through QueryMapper. This way, I created the following Mongo converter:

public class CustomMongoConverter extends MappingMongoConverter {
    public CustomMongoConverter(MongoDbFactory mongoDbFactory, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
        super(mongoDbFactory, mappingContext);
        conversionService.addConverter(new Converter<String, ObjectId>() {
            @Override
            public ObjectId convert(String source) {
                throw new RuntimeException();
            }
        });
    }
}

And then, I passed that implementation of the converter to MongoTemplate:

<bean id="mongoConverter" class="com.abcompany.model.repositories.utils.CustomMongoConverter">
    <constructor-arg ref="mongoDbFactory"/>
    <constructor-arg>
        <bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
    </constructor-arg>
</bean>

<bean class="org.springframework.data.mongodb.core.MongoTemplate" id="mongoTemplate">
    <constructor-arg ref="mongoDbFactory"/>
    <constructor-arg ref="mongoConverter"/>
</bean>

This way, when trying to convert from String to ObjectId, it throws an exception and it doesn't do it. Please note that you probably can just remove the converter from conversionService.