1

I am trying to store the location in MongoDB database. So far that I created a schema which contains field -

lastLocation: {
type: [Number, Number],
index: '2dsphere'
}, 

So when I try to inflate the values by using postman all other fields are stored in the database but location is not stored.

I have tried sending raw data given below -

{ ...."lastLocation": [123.443,123.342],.....}

and got the following error -

can\'t project geometry into spherical CRS: [ 123.443,123.342 ]

I want dummy data which I can send in postman as raw data. So, can anybody give me the raw data? Please also suggest if I am wrong in some other part of nodejs code as well. I am using mongoose here.

Thanks

mihai
  • 37,072
  • 9
  • 60
  • 86
  • Sorry but it's a little unclear what your question is actually asking here. Are you actually trying to "insert" new data to MongoDB and it's complaining that the coordinates supplied are invalid? If so then you probably should include the actual data as sent, the code doing the insertion and the "full" error exactly as returned from the driver response. If all in fact you are asking for is "dummy data" which is valid. Then look up your current location on google maps and get the coordinates. Those will be valid, as will it be simple to actually look for sample data sets. – Neil Lunn Nov 02 '18 at 06:32
  • I am asking for the json data format and not concerned about what should be the values. – Ayush Mahajan Nov 02 '18 at 07:25
  • To be clear the "JSON format" is **not** your issue here. The simple fact is `123.443, 123.342` is not a valid spherical coordinates. But something like `[-122.5076404, 37.7576793 ]` actually is. That is what your error is complaining about. – Neil Lunn Nov 02 '18 at 07:35
  • I just changed lastLocation to location and everything worked! Strange!! – Ayush Mahajan Nov 02 '18 at 07:46

1 Answers1

0

Try with these changes. It might help you.

const location = {
    lastLocation: {
        type: {
            type: String,
            default: 'Point'
        },
        coordinates: []
    }
}
location.index({
    location: '2dsphere'
})
Ankit Manchanda
  • 562
  • 6
  • 21
  • These changes really appear to be copied from somebody else's answer. ( probably the [most original source here](https://stackoverflow.com/a/15626310/2313887) ) And whilst the whole "type/type" mongoose schema confusion is indeed an issue, it's not the issue the question here really appears to be about. – Neil Lunn Nov 02 '18 at 07:02
  • I just changed lastLocation to location and everything worked! Strange!! – Ayush Mahajan Nov 02 '18 at 07:45
  • But your comment helped me too. Thanks! – Ayush Mahajan Nov 02 '18 at 07:47