I'm using Spring Data for Mongo on an existing database. The previous application used plain strings for ids instead of ObjectId.
My problem is that Spring Data insists on converting the strings to ObjectId, which makes all queries by id to fail.
For example, when I do repository.findOne('')
, the query executed is { "_id" : { "$oid" : "50cf9f34458cf91108ceb2b4"}}
when it should be { "_id" : "50cf9f34458cf91108ceb2b4" }
Is there a way to avoid Spring Data to convert string ids to ObjectId?
Thanks!
Diego
I finally found a solution for this. Probably not the best option, but works.
What I did was remove the converter from
String
toObjectId
thatMongoTemplate
uses throughQueryMapper
. This way, I created the following Mongo converter:And then, I passed that implementation of the converter to
MongoTemplate
: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
.I've faced same problem and my solution was like below:
The idea is to prevent default converters registration.