I'm facing a mongoose error during post data updation. The error is:
MongoError: Can't extract geo keys
I've tried to find the reason and the solution by searching on google but still I didn't get any proper solution.
Posts Model
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
});
Post Controller
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());
});
Output is:
MongoError: Can't extract geo keys: { _id: ObjectId('5e8c661f274e8e57d8e03887'), geoLocation: { type: "Point", coordinates: [ -8.455697300000001, 114.5110151 ], _id: ObjectId('5e8d7f7c35b9d36d45b483a2') } } can't project geometry into spherical CRS: [ -8.455697300000001, 114.5110151 ]
a field named coordinates that specifies the object’s coordinates.
If specifying latitude and longitude coordinates, list the longitude first and then latitude:
Valid longitude values are between -180 and 180, both inclusive. Valid latitude values are between -90 and 90, both inclusive.
Which means this takes longitude at index 0 and latitude at index 1.
Try coordinates: [ 114.5110151,-8.4556973] }