9

I have spent doing such a straight forward thing. I just want to do a CRUD operation on a user model using nodejs, mongoose, restify stack. My mongo instance is on mongolab. The user should contain a "loc" field . User schema is as follows :

var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var userSchema = new Schema( {
email_id : { type: String,  unique: true },
password: { type: String},
first_name: String,
last_name: String,
age: String,
phone_number: String,
profile_picture: String,
loc: {
     type: {},
    coordinates: [Number]
}  
});
userSchema.index({loc:'2d'});
var User = mongoose.model('user', userSchema);
module.exports = User;

the rest api used to post is as follows :

create_user : function (req, res, next) {
var coords = [];
coords[0] = req.query.longitude;
coords[1] = req.query.latitude;

var user = new User(
    {
        email_id : req.params.email_id,
        password: req.params.password,
        first_name: req.params.first_name,
        last_name: req.params.last_name,
        age: req.params.age,
        phone_number: req.params.phone_number,
        profile_picture: req.params.profile_picture,
        loc: {
                type:"Point",
                coordinates: [1.0,2.0] // hardcoded just for demo
            }
    }
    ); 
user.save(function(err){ 
    if (err) { 
        res.send({'error' : err}); 
    }
        res.send(user);
    });  
return next();
},

Now when i do a POST call on curl -X POST http://localhost:3000/user --data "email_id=sdass@dfAadsfds&last_name=dass&age=28&phone_number=123456789&profile_picture=www.jakljf.com&longitude=1.0&latitude=2.0" I get the following error

{
error: {
code: 16804
index: 0
errmsg: "insertDocument :: caused by :: 16804 location object expected,           location array not in correct format"
op: {
email_id: "sdass@dfAadsfdsadkjhfasvadsS.com"
password: "sdass123DadakjhdfsfadfSF45"
first_name: "shaun"
last_name: "dass"
age: "28"
phone_number: "123456789"
profile_picture: "www.jakljf.com"
loc: {
coordinates: [2]
0:  1
1:  2
-
type: "Point"
}-
_id: "55efc95e0e4556191cd36e5e"
__v: 0
}-
}-    
}

The location field is giving problems as the POST call works just fine if i remove the loc field from model

Below are the hits/trials I did : 1) Change userSchema.index({loc:'2d'}); to userSchema.index({loc:'2dsphere'}); 2) Changing loc schema to everything given in Stackoverflow. I would like to know the right way to define this though. 3) Passing the hardcode 2d array but still it says Location object expected, location array not in correct format" what format is required for this ?

Any help in this regard is greatly appreciated. Thanks.

shaun
  • 185
  • 3
  • 12
  • Could you try again at actually listing out a document you have from the mongo shell using the `.pretty()` option please. The reason is that the document appears to be "borked" and I'm pretty sure it's not the error output that is doing that. The schema definition is incorrect for a GeoJSON type and is the likely cause of the "borked", though I'm a little surprised it works at all. – Blakes Seven Sep 09 '15 at 07:17
  • @BlakesSeven you are right, it does not work. I don't have any document which have this schema. My question is how to fix it? I am using loc:'2d' and legacy coordinates pairs. It should work right ? – shaun Sep 09 '15 at 08:50
  • Can you please show what a document currently looks like. That is what I asked for as it will help in fixing any problems already created. – Blakes Seven Sep 09 '15 at 08:52
  • So, without loc feild the document looks like `{ "_id": { "$oid": "55eff4cc4e1ba5b55050fa20" }, "email_id": "sdass@dfAadsfdsadkjhfasvadsS.com", "password": "sdass123DadakjhdfsfadfSF45", "first_name": "shaun", "last_name": "dass", "age": "28", "phone_number": "123456789", "profile_picture": "www.jakljf.com", "__v": 0 }` – shaun Sep 09 '15 at 08:58
  • There is an [edit](http://stackoverflow.com/posts/32472023/edit) link on your question, please use it. Also I "specifically want" to see the current "loc" field. You have an error when indexing that field, so it's actual structure in current documents is what I want to see. Something that looks like and "ideally is" the same document reported in the error. And please "use the shell" and don't confuse the issue with converted "types" like `$oid`. Here. Guessing the collection name is "users" `db.users.findOne({ "_id": ObjectId("55efc95e0e4556191cd36e5e") })` and then please edit the question. – Blakes Seven Sep 09 '15 at 09:03

2 Answers2

15

MongoDB 2d index requires the legacy coordinates pairs format, which is just an array of coordinates like [1, 2].

If you need GeoJSON support, please use the 2dsphere index.

userSchema.index({loc:'2dsphere'});
FelisCatus
  • 5,054
  • 2
  • 21
  • 25
  • Yup, Thats true. I am doing a loc:2d index and putting just an array of coordinates [1,2]. Still it is giving the error. – shaun Sep 09 '15 at 08:56
  • 1
    By legacy coordinates pairs I mean `{..., loc: [1,2]}`, where the coordinates are directly specified on the `loc` field. No `{type: "Point"}` as in GeoJSON. – FelisCatus Sep 09 '15 at 10:43
  • 4
    Also, once the index is defined as `2d`, it is not so easy to change it to `2dsphere` by just updating your code. You may want to use `mongo` shell to manually drop your old index on DB server, or delete the whole collection and try again. – FelisCatus Sep 09 '15 at 10:46
  • Thanks, I ended up cleaning all my indexes and building a GeoJson schema. – shaun Sep 10 '15 at 08:31
  • Their documentation is poorly written – Oliver Dixon Mar 28 '21 at 11:29
0

If you are using Spring Boot make sure you set the index type to 2DSphere:

@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) GeoJsonPoint location;
SDekov
  • 9,276
  • 1
  • 20
  • 50