我在一个MongoDB集合中保存了一些类似这样的帖子:
{
"_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文档中却找不到关于这个函数的文档。如果有人知道我如何做到这一点,或者我可以在哪里找到一些有关它的文件,将不胜感激。
到目前为止,我正在尝试这样做,其中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/?com.mongodb.MongoQueryException:查询失败,错误代码17007,错误消息‘无法执行查询:错误处理查询: ns=posts2.texts2 limit=0 skip=0 03-1701:34:05.489 1786-1805/?W/System.err: Tree: GEONEAR field=loc maxdist=100 isNearSphere=0 03-1701:34:05.489 1786-1805/?W/System.err:排序:{} 03-1701:34:05.489 1786-1805/?W/System.err: Proj:{} 03-1701:34:05.489 1786-1805/?W/System.err:计划器返回错误:无法在服务器$geoNear ds011439.mlab.com:11439上找到ds011439.mlab.com:11439查询的索引
发布于 2016-03-17 15:44:06
我认为你的错误在这里
MongoCursor<Document> cursor = collection.find(near("loc", 22.9548626, 40.6159144, 100.0, 0.0)).iterator();
在json格式中,"loc“字段的类型是: Point。near函数有另一个构造函数,它是near(String field, Point point, Double max, Double min),所以您应该使用Point而不是22.9548626, 40.6159144,。
您可以创建这样一个点:
Position position = new Position(22.9548626,40.6159144);
Point point = new Point(position);这里是文档化。
试试看。
发布于 2016-03-16 22:21:14
我向您发布shell查询,以帮助您理解$near的工作原理。
首先,您需要在要运行位置感知查询的集合上创建2dsphere索引。假设您的位置名是loc
db.loc.createIndex({loc:"2dsphere"})创建索引后,执行以下查询
db.loc.find(
{
loc:
{ $near :
{
$geometry: { type: "Point", coordinates: [ 40.617856, 22.954689 ] },
$maxDistance: 2000
}
}
}
)它将搜索2000米(2公里)的所有位置。
发布于 2019-08-21 09:42:31
首先,打开到数据库的shell连接并创建索引。
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);https://stackoverflow.com/questions/36043213
复制相似问题