Java Mongodb рядом с запросом - PullRequest
       41

Java Mongodb рядом с запросом

0 голосов
/ 27 февраля 2019

Коллекция пользователей:

"userID":"1",
"location" : [ 
        77.667248, 
        13.0258562
    ],
 "latlong" : {
        "type" : "point",
        "coordinates" : [ 
            77.667248, 
            13.0258562
        ]
    },

Java-запрос:

 Query mongoQuery1 = new Query();
            mongoQuery1.addCriteria(Criteria.where("latlong.coordinates").near(point));
            mongoTemplate.find(mongoQuery1, User.class,"places");


  Query mongoQuery2 = new Query();
            mongoQuery2.addCriteria(Criteria.where("location").near(point));
            mongoTemplate.find(mongoQuery2, User.class,"places");

Я использую два выше java-запроса для поиска ближайших пользователей, но получаю исключение, подобное приведенному ниже. Я создал индекс 2dsphereтакже на пользовательской коллекции.

Исключение:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.geo.Point] to type [org.bson.Document]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)

при вставке пользовательского объекта в коллекцию в это время я получаю такое же исключение

org.springframework.data.geo.Point point = new Point(12.885970,77.656180);
 User u=new User();
 u.setUserID("123");
 u.setPoint(point);
 mongoTemplate.insert(u,"places");

Пожалуйста, помогите мне. Спасибо.

1 Ответ

0 голосов
/ 13 марта 2019
Query q=new Query(Criteria.where("userID").ne(null));
        q.with(new PageRequest(user.getPaginationNumber(),2));
                             //longitude          //latitude
        Point p=new Point(user.getLoc().get(1),user.getLoc().get(0));
        NearQuery n=NearQuery.near(p);
        n.spherical(true);
        n.inKilometers();
        n.maxDistance(16); //16 kms
  n.num(10); //return only 10 objects
  n.query(q);

        GeoResults<User> results=mongoTemplate.geoNear(n,User.class,"User",User.class);
...