使用查询类的Java mongodb geonear

时间:2019-03-08 11:07:07

标签: mongodb spring-data-mongodb spring-mongodb spring-mongo mongodb-geospatial

我正在尝试不使用聚合来查找接近某个点的文档。我想使用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
    ]

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

索引:

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

请帮助我。谢谢

0 个答案:

没有答案