2

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

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

4

1 回答 1

3

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

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

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

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

尝试coordinates: [ 114.5110151,-8.4556973] }

于 2020-04-08T10:13:24.190 回答