Cha*_*jer 2 mongodb node.js geonear
我在发布数据更新期间遇到猫鼬错误。错误是:
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
});
Run Code Online (Sandbox Code Playgroud)
后置控制器
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());
});
Run Code Online (Sandbox Code Playgroud)
输出是:
MongoError:无法提取地理键:{_id:ObjectId('5e8c661f274e8e57d8e03887'),geoLocation:{类型:“点”,坐标:[-8.455697300000001,114.5110151],_id:ObjectId('5e8d7f7c35b9d36d45b48 3a2') } } 不能将几何体投影到球形 CRS 中: [ -8.455697300000001, 114.5110151 ]
名为坐标的字段,指定对象\xe2\x80\x99s 坐标。
\n\n如果指定纬度和经度坐标,请先列出经度,然后列出纬度:
\n\n有效的经度值介于 -180 到 180 之间(包含两者)。\n有效的纬度值介于 -90 到 90 之间(包含两者)。
\n\n这意味着这需要索引 0 处的经度和索引 1 处的纬度。
\n\n尝试coordinates: [ 114.5110151,-8.4556973] }