Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location in mongoose, mongoDB

Whenever I try to store a location in my mongodb it doesn't show, so I guess I'm doing something wrong. I can't find any documentation on how to store a location in mongoose so I'm just gonna ask it here.

I first create my model:

var eventSchema = mongoose.Schema({ 
    name : String,
    creator : String,
    location : { type: [Number], index: '2dsphere'},

});

Then I try to add it to my database:

var newevent = new event({ 
        name: name,
        creator: tokenUser, 
        location : { type: "Point", coordinates: [longitude, latitude] },
});

When I look in my database everything is stored except the location ...

like image 463
Laurenswuyts Avatar asked Nov 30 '14 21:11

Laurenswuyts


1 Answers

I fixed it myself.

I did this in my model:

loc :  { type: {type:String}, coordinates: [Number]},

Underneath I made it a 2dsphere index.

eventSchema.index({loc: '2dsphere'});

And to add data to it:

loc: { type: "Point", coordinates: [ longitude, latitude ] },
like image 138
Laurenswuyts Avatar answered Oct 15 '22 18:10

Laurenswuyts