我想显示给定距离内的骑手的位置,并根据原点对它们进行排序。
这就是数据的样子
{
"_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 }
]);
此查询基于所有位置点过滤数据,而与 locType 无关。
2dsphere 是 location.coordinates 上的索引
{
"v" : NumberInt(2),
"key" : {
"locations.coordinates" : "2dsphere"
},
"name" : "locations.coordinates_2dsphere",
"ns" : "projectName.documentName",
"background" : true,
"2dsphereIndexVersion" : NumberInt(3)
}