【问题标题】:MongoError: Can't extract geo keysMongoError:无法提取地理键
【发布时间】:2020-07-20 14:18:47
【问题描述】:

我在发布数据更新期间遇到猫鼬错误。 错误是:

MongoError:无法提取地理键

我已经尝试通过谷歌搜索找到原因和解决方案,但我仍然没有得到任何合适的解决方案。

帖子模型

const geoLocationSchema = new mongoose.Schema({
    type: { type: String, default: 'Point'},
    coordinates: {
        type: [Number],
        index: { type: '2dsphere', sparse: false },
    }
});

const postsSchema = new mongoose.Schema({
    post_title: String,
    geoLocation: geoLocationSchema,
}, {
    timestamps: true
});

后控制器

const Posts = require("../models/Posts");

var findPattern = {_id: "5e8c661f274e8e57d8e03887"}; 
var updatePattern = {   geoLocation: {
    type: "Point",
    coordinates: [-8.4556973, 114.5110151] }
};

Posts.updateOne(findPattern, updatePattern) .then(updateRes => {
    console.log("post updated"); 
}).catch(err => {
    console.log("error", err.toString()); 
});

输出为:

MongoError: 无法提取地理键:{ _id: ObjectId('5e8c661f274e8e57d8e03887'), geoLocation: { type: "Point", 坐标: [-8.455697300000001, 114.5110151], _id: ObjectId('5e8d7f7c35b9d36d45b4)8无法将几何投影到球形 CRS:[-8.455697300000001, 114.5110151]

【问题讨论】:

  • 检查您的纬度值,它们似乎超出范围。
  • @mehta-rohan Lat long 是正确的。这是给印度尼西亚巴厘岛的。

标签: node.js mongodb geonear


【解决方案1】:

一个名为坐标的字段,用于指定对象的坐标。

如果指定经纬度坐标,先列出经度再列出纬度:

有效的经度值介于 -180 和 180 之间,包括两个端点。 有效的纬度值介于 -90 和 90 之间,包括两个端点。

这意味着这需要索引 0 处的经度和索引 1 处的纬度。

试试coordinates: [ 114.5110151,-8.4556973] }

【讨论】:

  • 谢谢罗汉。我在我的代码中实现了它。我还想问你一件事是否需要为纬度和经度实现任何其他类型的逻辑,正如你所说有效的经度值在 -180 和 180 之间,包括两个值。有效的纬度值在 -90 到 90 之间(包括两个端点)?
  • 如果您是从传感器中选择这些值,那么您不必担心太多,除非您可以进行简单的范围检查。
猜你喜欢
  • 2020-07-31
  • 2019-04-06
  • 1970-01-01
  • 2017-04-17
  • 2015-05-25
  • 2019-06-17
  • 1970-01-01
  • 2019-04-14
  • 2019-06-21
相关资源
最近更新 更多