我的Mongo数据库中有以下GEO数据。
db.car.ensureIndex({"loc":"2d" , "name" :1})
db.car.save({ "name":"Toyota car", "affiliation":"Toyota", "loc":{"lon":55.93939251390387,"lat":-113.999}})
db.car.save({"name":"Honda car", "affiliation":"Honda", "loc":{"lon":58.93939251390387,"lat":-113.999}})
db.car.save({"name":"Skoda", "affiliation":"Skoda", "loc":{"lon":52.93939251390387,"lat":-113.999}})
db.car.save({"name":"Ford", "affiliation":"Ford", "loc":{"lon":45.93939251390387,"lat":-113.999}})
db.car.save({"name":"Audi SUV", "affiliation":"Audi", "loc":{"lon":35.93939251390387,"lat":-113.999}})
db.car.save({"name":"Benz", "affiliation":"Benz", "loc":{"lon":75.93939251390387,"lat":-113.999}})
db.car.save({"name":"Skoda", "affiliation":"Skoda", "loc":{"lon":50.93939251390387,"lat":-113.999}})
db.car.save({"name":"Benz", "affiliation":"Skoda", "loc":{"lon":51.93939251390387,"lat":-113.999}})
db.car.save({"name":"Skoda SUV", "affiliation":"Skoda", "loc":{"lon":50.93939251390387,"lat":-113.999}})
db.car.save({"name":"Honda", "affiliation":"Skoda", "loc":{"lon":55.93939251390387,"lat":-113.999}})
我正在尝试为给定的无线电设备获取最接近的“名称”。
当我运行以下查询时
db.car.find({"loc" : {"$within" : {"$center" : [[50.93939251390,-114],5]}}})
我得到了
enter code here
> db.car.find({"loc" : {"$within" : {"$center" : [[50.93939251390,-114],5]}}})
{ "_id" : ObjectId("501cc07eebb626e104d5a23b"), "name" : "Skoda", "affiliation" : "Skoda", "loc" : { "lon" : 50.93939251390387, "lat" : -113.999 } }
{ "_id" : ObjectId("501cc07eebb626e104d5a237"), "name" : "Skoda", "affiliation" : "Skoda", "loc" : { "lon" : 52.93939251390387, "lat" : -113.999 } }
{ "_id" : ObjectId("501cc07eebb626e104d5a23c"), "name" : "Benz", "affiliation" : "Skoda", "loc" : { "lon" : 51.93939251390387, "lat" : -113.999 } }
{ "_id" : ObjectId("501cc07febb626e104d5a23d"), "name" : "Skoda SUV", "affiliation" : "Skoda", "loc" : { "lon" : 50.93939251390387, "lat" : -113.999 } }
但我想在半径中检索唯一的“名称”,如下所示。
> db.car.find({"loc" : {"$within" : {"$center" : [[50.93939251390,-114],5]}}})
{ "_id" : ObjectId("501cc07eebb626e104d5a23b"), "name" : "Skoda", "affiliation" : "Skoda", "loc" : { "lon" : 50.93939251390387, "lat" : -113.999 } }
{ "_id" : ObjectId("501cc07eebb626e104d5a23c"), "name" : "Benz", "affiliation" : "Skoda", "loc" : { "lon" : 51.93939251390387, "lat" : -113.999 } }
{ "_id" : ObjectId("501cc07febb626e104d5a23d"), "name" : "Skoda SUV", "affiliation" : "Skoda", "loc" : { "lon" : 50.93939251390387, "lat" : -113.999 } }
如何在其中添加约束?
答案 0 :(得分:0)
您不能将这种分组约束添加到您的查询中(至少使用MongoDB 2.0.x),因此您必须在应用程序代码中迭代结果以提取所需内容。
在下面假设:
创建了一个示例这是使用JS shell:
// Hash to save results
> var cars = {}
// Find closest cars to given geo point using geoNear
> db.runCommand(
{ geoNear: 'car', near : [50.93939251390,-114], num:5}
).results.forEach(
function(doc) {
if (cars[doc.obj.name]) {
// Increment number of matches for this name
cars[doc.obj.name]['matches'] = cars[doc.obj.name]['matches'] + 1;
} else {
// Closest match found
doc.obj['matches'] = 1;
doc.obj['distance'] = doc.dis;
cars[doc.obj.name] = doc.obj;
}
}
)
// Check the results
> cars
{
"Skoda" : {
"_id" : ObjectId("501d108c9d2b5b2b5443712d"),
"name" : "Skoda",
"affiliation" : "Skoda",
"loc" : {
"lon" : 50.93939251390387,
"lat" : -113.999
},
"matches" : 2,
"distance" : 0.0010000000000047748
},
"Skoda SUV" : {
"_id" : ObjectId("501d108c9d2b5b2b5443712f"),
"name" : "Skoda SUV",
"affiliation" : "Skoda",
"loc" : {
"lon" : 50.93939251390387,
"lat" : -113.999
},
"matches" : 1,
"distance" : 0.0010000000000047748
},
"Benz" : {
"_id" : ObjectId("501d108c9d2b5b2b5443712e"),
"name" : "Benz",
"affiliation" : "Skoda",
"loc" : {
"lon" : 51.93939251390387,
"lat" : -113.999
},
"matches" : 1,
"distance" : 1.0000005000037404
},
"Ford" : {
"_id" : ObjectId("501d108c9d2b5b2b5443712a"),
"name" : "Ford",
"affiliation" : "Ford",
"loc" : {
"lon" : 45.93939251390387,
"lat" : -113.999
},
"matches" : 1,
"distance" : 5.000000099996134
}
}
此类搜索结果的常用方法是显示列表中找到的所有匹配项,并在地图上绘制它们。 理想情况下,您的地图视图会支持某种Marker Clustering,以根据用户选择的缩放级别自动对标记进行分组。