skip to Main Content

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


  1. 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<YourClassType> gym = database.getCollection(collectionName,YourClassType.class).aggregate(Arrays.asList(
                new Document("$addFields", new Document("_id", new Document("$toString", "$_id"))),
                new Document("$match", new Document("_id", byIdRequest.getId()))
        )).cursor();
    

    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.

    Login or Signup to reply.
  2. 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.

    enter image description here

    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.

    package thePackage;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.context.event.EventListener;
    import org.springframework.data.mongodb.CodecRegistryProvider;
    import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MongoSetup{
        private final MappingMongoConverter mappingMongoConverter;
        private final CodecRegistryProvider codecRegistryProvider;
    
        @Autowired
        public MongoSetup(MappingMongoConverter mappingMongoConverter, CodecRegistryProvider codecRegistryProvider) {
            this.mappingMongoConverter = mappingMongoConverter;
            this.codecRegistryProvider = codecRegistryProvider;
        }
    
        @EventListener(ContextRefreshedEvent.class)
        public void initMongo() {
            mappingMongoConverter.setCodecRegistryProvider(codecRegistryProvider);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search