MongoError:无法提取地理位置键

时间:2020-04-08 08:10:50

标签: node.js mongodb geonear

在更新数据后,我遇到猫鼬错误。 错误是:

MongoError:无法提取地理键

我试图通过在Google上进行搜索来找到原因和解决方案,但仍然没有找到合适的解决方案。

帖子模型

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:{类型:“点”,坐标:[-8.455697300000001,114.5110151],_id:ObjectId('5e8d7f7c35b9d36d45b483a2')无法将几何投影到球形CRS中:[-8.455697300000001,114.5110151]

1 个答案:

答案 0 :(得分:2)

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

如果指定纬度和经度坐标,请先列出经度,然后再列出纬度:

有效的经度值在-180到180之间(包括两者)。 有效的纬度值在-90到90之间(包括两者)。

这表示在索引0处取经度,在索引1处取经度。

尝试coordinates: [ 114.5110151,-8.4556973] }