使用 2d 索引,与 $geoNear 聚合

Posted

技术标签:

【中文标题】使用 2d 索引,与 $geoNear 聚合【英文标题】:Using 2d Indexes, in aggregate with $geoNear 【发布时间】:2016-07-03 13:17:00 【问题描述】:

我正在尝试将 geoNear 与聚合一起使用,但我收到此错误消息:

errmsg: "'near' field must be point"

因为我的位置字段是 [Number]:

var locationSchema = mongoose.Schema(
name            : String,
addressText     : String,
location    : [Number] // [<longitude>, <latitude>] 
);
module.exports = mongoose.model('Location', locationSchema );

我试图像这样聚合:

Location.aggregate([
            $geoNear: 
                near: type : "Point", "location": [ 48.2, 16.3 ],
                limit: 1000000,
                distanceField: "dist.calculated",
                includeLocs: "true",
                maxDistance: 10
            
        ])
        .exec()

我知道我必须添加我的位置type:"Point",但不幸的是为时已晚。我可以用其他方式解决这个问题吗?

【问题讨论】:

【参考方案1】:

尝试将聚合中的“位置”替换为“坐标”。

【讨论】:

以上是关于使用 2d 索引,与 $geoNear 聚合的主要内容,如果未能解决你的问题,请参考以下文章