1

我试图在不使用聚合的情况下找到靠近某个点的文档。我想使用 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)

用户 4 集合:

   "userID" : "1",
    "user_Email" : "ghjkl@gmail.com",
    "loc" : [ 
        77.618843, 
        13.052758
    ]

"userID" : "2",
    "user_Email" : "abc@gmail.com",
    "loc" : [ 
        77.618743, 
        13.054518
    ]

我在 user4 集合中的 loc 属性上创建了 2dsphere 索引。

指数:

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

请帮助我。谢谢

4

0 回答 0