首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按$geoNear(目标)过滤,按$geoNear (源)排序

按$geoNear(目标)过滤,按$geoNear (源)排序
EN

Stack Overflow用户
提问于 2018-09-02 16:58:11
回答 1查看 393关注 0票数 1

我想要显示在给定距离内的车手的位置,并根据原点对它们进行排序。

下面是数据的外观

代码语言:javascript
复制
{ 
"_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***
    }
], 

}

这是我到目前为止所做的作业。

代码语言:javascript
复制
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上的索引

代码语言:javascript
复制
{ 
"v" : NumberInt(2), 
"key" : {
    "locations.coordinates" : "2dsphere"
}, 
"name" : "locations.coordinates_2dsphere", 
"ns" : "projectName.documentName", 
"background" : true, 
"2dsphereIndexVersion" : NumberInt(3)

}

EN

回答 1

Stack Overflow用户

发布于 2018-09-04 19:25:48

我用一种不同的方法解决了这个问题。

首先,我将距离字段添加到数据中。将源和目标设置为单独的对象,而不是数组。

代码语言:javascript
复制
{ 
"_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 1KM的原始场和距离小于200 1KM的原始场进行了geoNear查询。

代码语言:javascript
复制
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 }
]);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52135435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档