Mongoose Location, mongoDB

Whenever I try to save a place in my mongodb, it is not displayed, so I think I'm doing something wrong. I can not find the documentation on how to save the location in the mongoose, so I will just ask it here.

First I 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 for the location ...

+4
source share
2 answers

I fixed it myself.

I did this in my model:

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

Under this, I made the 2dsphere index.

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

And add data to it:

loc: { type: "Point", coordinates: [ longitude, latitude ] },
+6
source

, (),

: http://mongoosejs.com/docs/api.html#schematype_SchemaType-index

Object, Boolean, String

, ,

var eventSchema = new Schema({ 
        location: { type: [Number], index: { type: '2dsphere', sparse: true}}
)

.

+1

All Articles