Mongodb 根据$geoNear(目的地)进行筛选,并根据$geoNear(起点)进行排序
我想显示给定距离内的车手位置,并根据原点对其进行排序 这就是数据的样子Mongodb 根据$geoNear(目的地)进行筛选,并根据$geoNear(起点)进行排序,mongodb,mongoose,geospatial,geojson,geonear,Mongodb,Mongoose,Geospatial,Geojson,Geonear,我想显示给定距离内的车手位置,并根据原点对其进行排序 这就是数据的样子 { "_id" : ObjectId("5b5a9cd706f9b02068ebc4a6"), "name" : "Bangalore to hyderabad", "locations" : [ { "coordinates" : [ 77.5945627, 12.9715987 ], "_id" : Objec
{
"_id" : ObjectId("5b5a9cd706f9b02068ebc4a6"),
"name" : "Bangalore to hyderabad",
"locations" : [
{
"coordinates" : [
77.5945627,
12.9715987
],
"_id" : ObjectId("5b5a9cd706f9b02068ebc4a8"),
"formattedAddress" : "Bengaluru, Karnataka, India",
"name" : "Bengaluru",
"type" : "Point",
"googlePlaceId" : "5b0d9fd719c9616d747b8a0d",
"placeType" : "origin" //***Sort by this***
},
{
"coordinates" : [
78.486671,
17.385044
],
"_id" : ObjectId("5b5a9cd706f9b02068ebc4a7"),
"formattedAddress" : "Hyderabad, Telangana, India",
"name" : "Hyderabad",
"type" : "Point",
"googlePlaceId" : "5b0d9fd719c9616d747b8a0d",
"locType" : "destination" // *****Filter by this***
}
],
}
这是我到目前为止做的家庭作业
return await Ride.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ 77.5946,12.8716 ] },
distanceField: "distance",
maxDistance: 20000000, //200Kms
query: { private: false },
spherical: true
}
},
// { "$sort": { "distance": 1 } },
{ "$skip": 0 },
{ "$limit": 30 }
]);
此查询基于所有位置点过滤数据,而不考虑位置类型
2dsphere是location.coordinates上的索引
{
"v" : NumberInt(2),
"key" : {
"locations.coordinates" : "2dsphere"
},
"name" : "locations.coordinates_2dsphere",
"ns" : "projectName.documentName",
"background" : true,
"2dsphereIndexVersion" : NumberInt(3)
}我用另一种方法解决了这个问题 首先,我将“距离”字段添加到数据中。将原点和目标作为单独的对象而不是数组
{
"_id" : ObjectId("5b5a9cd706f9b02068ebc4a6"),
"name" : "Bangalore to hyderabad",
"origin" : {
"coordinates" : [
77.5945627,
12.9715987
],
"_id" : ObjectId("5b5a9cd706f9b02068ebc4a8"),
"formattedAddress" : "Bengaluru, Karnataka, India",
"name" : "Bengaluru",
"type" : "Point",
"googlePlaceId" : "5b0d9fd719c9616d747b8a0d",
},
destination: {
"coordinates" : [
78.486671,
17.385044
],
"_id" : ObjectId("5b5a9cd706f9b02068ebc4a7"),
"formattedAddress" : "Hyderabad, Telangana, India",
"name" : "Hyderabad",
"type" : "Point",
"googlePlaceId" : "5b0d9fd719c9616d747b8a0d",
}
然后我在半径为1公里的原始场上查询发射geoNear,查询距离小于200公里
Ride.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ 77.5946,12.8716 ] },
distanceField: "distanceFromOrigin",
maxDistance: 1000, //1Km
query: { private: false, distance:{$lt:200}},
spherical: true
}
},
// { "$sort": { "distanceFromOrigin":1,"distance": 1 } },
{ "$skip": 0 },
{ "$limit": 30 }
]);
定义更好的方法这是解决这个问题的正确方法吗?在这个网站上,自以为是的问题是离题的。你需要用客观、可衡量的术语来定义“更好”或“正确”。没错,我重新定义并删除了固执己见的部分。我试图实现,排序的位置根据原点和目的地的半径过滤。类似于“向我显示从我附近开始并在我周围行驶200公里的列车列表”的答案,那么,您想显示距离某个点的限制范围内(例如200公里)的位置,按给定点的距离升序排列吗?