我的 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 } }
如何在其中添加约束?