1

I am trying to find the documents which are near to a point without using aggregation.I want use NearQuery or Query class or any other.

I tried with following queries but getting exception

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");

Exception:

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 collection:

   "userID" : "1",
    "user_Email" : "[email protected]",
    "loc" : [ 
        77.618843, 
        13.052758
    ]

"userID" : "2",
    "user_Email" : "[email protected]",
    "loc" : [ 
        77.618743, 
        13.054518
    ]

I have created 2dsphere index on loc attribute in user4 collection.

index:

{
        "v" : 2,
        "key" : {
            "loc" : "2dsphere"
        },
        "name" : "loc_2dsphere",
        "ns" : "CitizenChat4.User4",
        "2dsphereIndexVersion" : 3
    } 

Please help me.Thankyou

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.