Я спасаю некоторые сообщения в виде 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 java docs. Если кто-нибудь знает, как я могу это сделать или где я могу найти документацию об этом, мы будем очень благодарны.
Это то, что я пытаюсь до сих пор где texts
это коллекция из БД:
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();
}
UPDATE
После небольшого исследования i пришли к следующему:
я изменяет свои 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: Query failed with error code 17007 and error message 'Unable to execute query: error processing query: ns=posts2.texts2 limit=0 skip=0 03-17 01:34:05.489 1786-1805/? W/System.err: Tree: GEONEAR field=loc maxdist=100 isNearSphere=0 03-17 01:34:05.489 1786-1805/? W/System.err: Sort: {} 03-17 01:34:05.489 1786-1805/? W/System.err: Proj: {} 03-17 01:34:05.489 1786-1805/? W/System.err: planner returned error: unable to find index for $geoNear query' on server ds011439.mlab.com:11439
Где ваш код Java? – Saleem
@Saleem я редактировал вопрос.Спасибо за ваш ответ –
В моем json тоже есть изменения. «lng» и «lat» находятся в «loc» –