2

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 ]

James Z
  • 12,209
  • 10
  • 24
  • 44
Chandan Chhajer
  • 303
  • 2
  • 7

2 Answers2

5

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] }

mehta-rohan
  • 1,373
  • 1
  • 14
  • 19
  • Thank you Rohan. I implemented it in my code. One more thing I want to ask to you Is there any other kind of logic needs to be implemented for latitude and longitude as you said Valid longitude values are between -180 and 180, both inclusive. Valid latitude values are between -90 and 90, both inclusive? – Chandan Chhajer Apr 08 '20 at 12:34
  • If you are picking these values from sensors then you don't need to worry much, except you can have simple range checks in place. – mehta-rohan Apr 08 '20 at 13:30
1

check this image

Note: userSchema.index({ location: '2dsphere' })

M Surya
  • 54
  • 6
  • 1
    Please post your code directly to the post, no need of adding extra URLs that can become invalid in future. – Tyler2P May 25 '22 at 15:10