node.js - Mongoose 地理查询未返回正确的结果

标签 node.js mongodb mongoose geolocation

我正在尝试查询一个数据库,其中我存储了某些坐标附近位置的坐标,并指定了 maxDistance。 我在官方 mongo 文档中读到 maxDistance 以米为单位。 集合架构如下:

var BranchSchema = new Schema({
parentId: {
    type: Schema.Types.ObjectId,
    required: 'The ID of the parent is required.',
    index: true
},
name: {
    type: 'String',
    required: 'The branch name is required.'
},
location: {
    type: [Number],
    index: {
        type: '2dsphere'
    }
}
});

我插入了一个包含以下信息的文档:

{
"parentId" : ObjectId("54ee08c2d903aca72291f120"),
"name" : "Branch1",
"_id" : ObjectId("54ee422809f242122744990c"),
"location" : [ 
    33.377796, 
    35.480911
]
}

然后我尝试查询 lat=33.901948 和 long=35.576797,最大距离为 5。 我使用了网络上的在线工具( http://www.movable-type.co.uk/scripts/latlong.html ),它给出了 lat=33.901948 和 long=35.576797 以及 lat=33.377796 和 long=35.480911 之间的距离为 58KM,显然大于 5 米,并且查询仍然返回结果不应该

Mongoose 查询如下:

 Branch.where('location').near({
    center: [lat, long],
    maxDistance: proximity,
    spherical: true
}).exec(function (err, branches) {
    if (err) {
        return res.status(400)
            .send({
                message: errors.getErrorMessage(err)
            });
    }
    return res.json(branches);
});

提前致谢

最佳答案

实际上,我在您的问题上发现了一些错误;

1- 索引。

 - 2dsphere index if specifying a GeoJSON point
 - 2d index if specifying a point using legacy coordinates.

您的架构使用旧坐标字段。它不是 GeoJSON 字段。因为 GeoJSON 必须包含一个坐标类型值,如下所示;

location: {
            'type': { type: String, default: 'Point' },
             coordinates: [Number]
           } 

如果您想要遗留坐标字段,您应该使用2d索引。

2- lat 的顺序。和液化天然气。 您的代码必须 start with Longitude

IMPORTANT
Specify coordinates in this order: “longitude, latitude.”

另一方面,如果您想使用旧版二维索引,您可以使用 the following codes ;

{ location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }

上述代码有一个$maxDistance参数,用于指定半径。我认为你应该检查this出去。因为您必须考虑以下线路才能找到 5 米的接近度。

5 米 = (5/6371000) 地球半径

所以,我认为以下代码有效;

Branch.where('location').near({
    center: [long, lat],
    maxDistance: 5/6371000,
    spherical: true
}).exec(function (err, branches) {
    if (err) {
        return res.status(400)
            .send({
                message: errors.getErrorMessage(err)
            });
    }
    return res.json(branches);
});

或者

Branch.find(
    { location : { $near : [ -73.9667, 40.78 ], $maxDistance: 5/6371000 }}, 
    function (err, branches) {
        if (err) {
            return res.status(400)
            .send({
                message: errors.getErrorMessage(err)
            })
        }
        return res.json(branches);
    }
)  

希望这对您有所帮助,祝您好运!

关于node.js - Mongoose 地理查询未返回正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731587/

相关文章:

javascript - 仅在 mongoose .find() 上返回文档 _id

java - 为什么 node.js 加密模块给出的结果与 AES 加密的 Java Cipher 类不同?

arrays - Mongo 查询 - 推送唯一项并保留数组中的顺序

node.js - redis/memcached 缓存与 Etag 缓存有何不同?

node.js - mongodb 使用 mongoose : E11000 duplicate key error on . 保存嵌套集合

mongodb - 如何通过查询子文档使用数组作为过滤器

mongodb - 如何从数组内的文档中投影特定字段?

Mongoose.js默认情况下来自查询结果的_id和__v除外

node.js - Youtube API无法同时创建直播和直播的两个绑定(bind)(1对1)

javascript - 在nodejs中使用 Angular 前端