MongoDB - Java |在

时间:2016-03-16 17:48:28

标签: java mongodb location

我在MongoDB Collection中保存了一些帖子,如下所示:

{
    "_id": {
        "$oid": "56e7152edbaacc2ab1d34f20"
    },
    "title": "the title",
    "body": "the post",
    "loc": {
       "lat": 40.616856,
       "lng": 22.954689
    },
    "timestamp": "1457984814748",
    "username": "User",
    "fbID": "4324"
}

我希望获得特定比率的帖子,比如2公里。

我试图找到$near如何工作,但在mongodb java docs中找不到关于此函数的文档。如果有人知道我该怎么做,或者我在哪里可以找到一些关于它的文档将非常感激。

这就是我到目前为止所尝试的texts是db:

的集合
        QueryBuilder query = new QueryBuilder();
        query.put("loc").near(22.9545545, 40.616479, 50);
        MongoCursor<Document> cursor2 = texts.find((BasicDBObject) query.get()).iterator();
        try {
            while (cursor2.hasNext()) {
                Document doc = cursor2.next();
                documents.add(doc);
            }
        } finally {
            cursor2.close();
        }

我也试过这个:

Double locationLongitude = new Double (22.9545545);
                Double locationLatitude = new Double (40.616479);
                BasicDBObject  locQuery = new BasicDBObject ();
                locQuery.put("loc", new Document("$near", new Double[]{locationLongitude, locationLatitude}));
MongoCursor<Document> cursor2 = texts.find(locQuery).iterator();
            try {
                while (cursor2.hasNext()) {
                    Document doc = cursor2.next();
                    documents.add(doc);
                }
            } finally {
                cursor2.close();
            }

更新

经过一番研究后,我来到了这里:

我将我的json文档更改为:

{
    "_id": {
        "$oid": "56e9e7440296451112edd05d"
    },
    "title": "ρ",
    "body": "ρ",
    "lng": 22.954689,
    "lat": 40.616856,
    "loc": {
        "type": "Point",
        "coordinates": [
            22.954689,
            40.616856
        ]
    },
    "timestamp": "1458169668154",
    "username": "User",
    "fbID": "4324"
}

和我的java代码:

        collection.createIndex(new Document("loc", "2dsphere"));
        MongoCursor<Document> cursor = collection.find(near("loc", 22.9548626, 40.6159144, 100.0, 0.0)).iterator();

        try {
            while (cursor.hasNext()) {
                Document doc = cursor.next();
                documents.add(doc);
            }
        } finally {
            cursor.close();
        }

但仍然没有得到任何结果..游标没有文件。 我也收到了这个错误

  

03-17 01:34:05.489 1786-1805 /? W / System.err:com.mongodb.MongoQueryException:查询失败,错误代码为17007,错误消息“无法执行查询:错误处理查询:ns = posts2.texts2 limit = 0 skip = 0   03-17 01:34:05.489 1786-1805 /? W / System.err:树:GEONEAR字段= loc maxdist = 100 isNearSphere = 0   03-17 01:34:05.489 1786-1805 /? W / System.err:排序:{}   03-17 01:34:05.489 1786-1805 /? W / System.err:Proj:{}   03-17 01:34:05.489 1786-1805 /? W / System.err:planner返回错误:无法在服务器上找到$ geoNear查询的索引ds011439.mlab.com:11439

3 个答案:

答案 0 :(得分:0)

我发布了您的shell查询,以帮助您了解$近乎工作的方式。

首先,您需要在要运行位置感知查询的集合上创建2dsphere索引。假设您的位置名称为loc

db.loc.createIndex({loc:"2dsphere"})

创建索引后,执行以下查询

db.loc.find(
   {
     loc:
       { $near :
          {
            $geometry: { type: "Point",  coordinates: [ 40.617856, 22.954689 ] },
            $maxDistance: 2000
          }
       }
   }
)

它将搜索2000米(2公里)

的所有位置

答案 1 :(得分:0)

我认为你的错误在这里 MongoCursor<Document> cursor = collection.find(near("loc", 22.9548626, 40.6159144, 100.0, 0.0)).iterator();

在你的json格式中,你的“loc”字段是type:Point。 near函数有另一个构造函数 near(String field, Point point, Double max, Double min) 因此,您应该使用22.9548626, 40.6159144,而不是Point

您可以像这样创建一个Point:

Position position = new Position(22.9548626,40.6159144);
Point point = new Point(position);

Here是文件。

试一试。

答案 2 :(得分:0)

首先打开一个到数据库的外壳连接,然后创建索引。

db.Salons.createIndex({"loc":"2dsphere"});

当您要索引嵌套对象时:

db.Salons.createIndex({"address.loc":"2dsphere"});

这是查询$ near的另一种方式

//Imports
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
    ....

在某个地点附近找到沙龙

Point point  = new Point(longitude, latitude);
List<Salon> salonsNear = mongoTemplate.find(new Query(Criteria
                              .where("loc")
                              .near(point)
                              .maxDistance(100)), Salon.class);

在2公里半径内查找

Double longitude = 4.368333;
Double latitude = 51.1981366;
Double distance  = 2.0;

Circle myCircle = new Circle(new Point(longitude, latitude), new Distance(distance, Metrics.KILOMETERS));
List<Salon> salonsWithinCircle = mongoTemplate.find(new Query(Criteria
                                   .where("loc")
                                   .withinSphere(myCircle)), Salon.class);

在2公里半径内查找嵌套对象

Circle myCircle = new Circle(new Point(longitude, latitude), new Distance(distance, Metrics.KILOMETERS));
List<Salon> salonsWithinCircle = mongoTemplate.find(new Query(Criteria
                                   .where("address.loc")
                                   .withinSphere(myCircle)), Salon.class);