Я пытаюсь найти документы, которые находятся близко к точке, без использования агрегации. Я хочу использовать класс NearQuery или Query или любой другой.
Я пытался выполнить следующие запросы, но получил исключение
1)
NearQuery n=NearQuery.near(user.getLoc().get(0),user.getLoc().get(1));
GeoResults<User> results=mongoTemplate.geoNear(n,User.class,"User4",User.class);
2)
Point point= new Point(77.6672961,13.0257889);
Query query= new Query(Criteria.where("loc").nearSphere(point));
List<User> nearbyusers=mongoTemplate.find(query, User.class,"User4");
3)
Point p=new Point(13.0257889,77.6672961);
Query q=new Query(Criteria.where("loc").near(p));
List<User> i=mongoTemplate.find(q,User.class,"User4");
Исключение:
org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 2 (BadValue): 'error processing query: ns=CitizenChat4.User4 limit=100Tree: GEONEAR field=loc maxdist=1.79769e+308 isNearSphere=0
Sort: {}
Proj: { $pt: { $meta: "geoNearPoint" }, $dis: { $meta: "geoNearDistance" } }
planner returned error: unable to find index for $geoNear query' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "error processing query: ns=CitizenChat4.User4 limit=100Tree: GEONEAR field=loc maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: { $pt: { $meta: \"geoNearPoint\" }, $dis: { $meta: \"geoNearDistance\" } }\n planner returned error: unable to find index for $geoNear query", "code" : 2, "codeName" : "BadValue" }; nested exception is com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'error processing query: ns=CitizenChat4.User4 limit=100Tree: GEONEAR field=loc maxdist=1.79769e+308 isNearSphere=0
Sort: {}
Proj: { $pt: { $meta: "geoNearPoint" }, $dis: { $meta: "geoNearDistance" } }
planner returned error: unable to find index for $geoNear query' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "error processing query: ns=CitizenChat4.User4 limit=100Tree: GEONEAR field=loc maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: { $pt: { $meta: \"geoNearPoint\" }, $dis: { $meta: \"geoNearDistance\" } }\n planner returned error: unable to find index for $geoNear query", "code" : 2, "codeName" : "BadValue" }
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:138)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2756)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:510)
Коллекция User4:
"userID" : "1",
"user_Email" : "ghjkl@gmail.com",
"loc" : [
77.618843,
13.052758
]
"userID" : "2",
"user_Email" : "abc@gmail.com",
"loc" : [
77.618743,
13.054518
]
Я создал 2dsphere index для атрибута loc в коллекции user4.
Индекс:
{
"v" : 2,
"key" : {
"loc" : "2dsphere"
},
"name" : "loc_2dsphere",
"ns" : "CitizenChat4.User4",
"2dsphereIndexVersion" : 3
}
Пожалуйста, помогите мне. Спасибо