【问题标题】:Mongo DB - Geospatial Queries with location and field radius(Internal field comparision)Mongo DB - 具有位置和字段半径的地理空间查询(内部字段比较)
【发布时间】:2016-03-01 10:46:36
【问题描述】:

在我的项目中有两个需求

第一个:我有下面的集合

{
   "name": "James",
   "loc" : [ 12.9000, 14.6733]
},
{
   "name": "James",
   "loc" : [ 54.9000, 78.6733]
}

为此,我必须找到与我的位置匹配的所有位置和特定半径 所以我使用这个查询:

var myLoc     = [ 13.5,  67.5 ];
var myRadius  = 150;
model.find({
        loc : {
            $geoWithin : {
                $centerSphere : [ myLoc, myRadius ]
            }
        }
    }
).exec()

上述查询工作正常。

第二个:我有如下收藏。我遇到了问题。

{
   "name"   : "James",
   "loc"    : [ 12.9000, 14.6733],
   "radius" : 115
},
{
   "name"   : "James",
   "loc"    : [ 54.9000, 78.6733],
   "Radius" : 276
}

这里我的收藏本身就有半径。 用户输入仅是 loc

var myLoc = [ 14.67, 56.78 ];

现在我必须找到所有与我的位置相匹配的文档,它们的位置和半径。

我尝试过类似的查询

model
.where('loc').within({ center: myLoc, radius: 'this.Radius', unique: true, spherical: true })
.exec()

Error : The err is CastError: Cast to number failed for value "this.Radius" at path "undefined"

然后

model
    .find(
        {
            $where: 
                {
                    Location : { 
                        $geoWithin : 
                            { $centerSphere : [myLoc, '$Radius'] }
                        }
                }
        }
    )
    .exec()


Error : Error: Must have a string or function for $where

然后

model
    .aggregate( 
        { 
            $match : {
                loc : {
                    $geoWithin : {
                        $centerSphere : [ myLoc, "$Radius" ]
                    }
                }   
            }                                       
        }
     )
    .exec(  )

错误:错误是 MongoError:异常:错误查询:BadValue 错误地理查询:{ $geoWithin: { $centerSphere: [ [ -1.965373600000021, 52.4744464 ], "$Radius" ] } }

我对 mongodb 查询很陌生。即使您的小建议也会对我有很大帮助。 请告诉我如何实现这一目标。 非常感谢您的帮助。谢谢

【问题讨论】:

  • 您好,对于同一个用例,我遇到了同样的问题,您是如何解决的?以下答案已弃用

标签: node.js mongodb mongoose geospatial


【解决方案1】:

您可以使用$where 运算符:

model.find({'$where': function() {
    var myLoc = [ 14.67, 56.78 ];
    return { 'loc': { 
        '$geoWithin': { "$centerSphere": [ myLoc, this.Radius ] } }
    }}
})

【讨论】:

  • 从 MongoDB 4.4 开始,$where 不再支持已弃用的具有范围的 BSON 类型 JavaScript 代码(BSON 类型 15)。 $where 运算符仅支持 BSON 类型 String(BSON 类型 2)或 BSON 类型 JavaScript(BSON 类型 13)。自 MongoDB 4.2.1 起,不推荐使用具有 $where 范围的 BSON 类型 JavaScript。
【解决方案2】:

样本收集数据

{ 
"business": "Store",
"geo": {
    "name": "StoreName",
    "coordinates": [80.628913, 13.622177],
    "type": "Point"
},
"address": "Some Address",
"city": "Kolkata",
"pincode": "700001",
"startTime": "10:00:00 AM",
"closingTime": "7:00:00 PM",
"closedOn": "Closed on Sunday",}

创建2dSphear index on db.collectionName.createIndex({geo:'2dsphere'}) 现在使用$geoNear 聚合查询来平滑获取记录。

db.collectionName.aggregate([
       {
         $geoNear: {
            near: { type: "Point", coordinates: [  80.3355,13.6701  ] }, // Mandatory Param.
            distanceField: "dist.calculated",          // It will return in the document how much distence (meter) is far feom center
            maxDistance: 2000,                         // Fetch 2KM radius store, Distance in meter from the center 
            spherical: true,                           // calculates distances using spherical geometry.
            query:{pincode:"700001"}                  // Specific Query parameter on other field present in the collection
         }
       }
    ]).pretty()

这是一个工作代码,经过测试和验证。

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2016-08-29
    相关资源
    最近更新 更多