java mongodb 附近查询
java mongodb near query
用户合集:
"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 查询来获取附近的用户,但遇到这样的异常 below.I 还在用户集合上创建了 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");
请帮忙me.Thankyou。
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);
用户合集:
"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 查询来获取附近的用户,但遇到这样的异常 below.I 还在用户集合上创建了 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");
请帮忙me.Thankyou。
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);