the index created in mongo database
Field in the entity
@Indexed(name = "2dsphere")
private Point location;
This field in mongo db
"location":
{ "type": "Point",
"coordinates":
[ 41.035137, 28.98953 ]
}
Try to retrieve entity from mongo db
repository.findById(id).orElse(null) throws the below error
ex.getMessage(): Expected to read Document Document{{type=Point, coordinates=[41.015137, 28.97953]}} into type class com.mongodb.client.model.geojson.Point but didn’t find a PersistentEntity for the latter!
2
Answers
if you use repository by hibernate or etc. java will try mapping Document and your Class type. Unfortunately this way is so slow and not an optimized way
if you use native query. mongo returns Document type. if you want listing or pageable then Data type is insignificant. because mongo already return json type. also you want to return any response object then use can use $project stage.
but if you wanna take an entity and update it and save it. you need own data type. then you can use MongoCursor right now.
what is MongoCursor ? mongo cursor just like a collection. stores data of the given type. and this way is very fast as native query.
example:
MongoCursor doc : https://www.mongodb.com/docs/manual/reference/method/js-cursor/
important point ! : you must add first add Fields stage because you storage ObjectId Type but your entity has String id Field. dont forget this stage.
Just had to troubleshoot the same issue. I found out that the MappingMongoConverter was looking for the codecRegistryProvider to provide a codec for the Geometry type but the codecRegistryProvider was null.
I am not sure why it does not get set, but I resolved the issue by creating a component that waits for the ContextRefreshedEvent and sets the codecRegistryProvider manually.