Dimitris
Dimitris

Reputation: 93

Mongoose schema updating GeoJSON

I have the following Mongoose schema representing a moving object

var vehicleSchema = new Schema({
    properties:{
        obj:String,
        name:String,
        id:String
    },
    geometry : {
        type: {type : String},
        coordinates : [ Number, Number ]
    }
});

and I am trying to update it with the following object and command which work flawlessly if I enter in mongo console but does not update the document when used in a node function.

This is the object to be updated:

var updatedVehicle = new Vehicle(
    {properties:{
        obj:"Answer",
        name:"is", 
        id:"42"
    },
    geometry:{
        type:"Point", 
        coordinates:[42,42]
    }
 })

And this is the update command

Vehicle.update(
   {$and:[
       {'properties.obj':data.properties.obj},
       {'properties.id':data.properties.id}
    ]}, 
   {$set:
       {properties:data.properties, geometry:data.geometry}
   }, 
   {upsert: true}, 
   cb
)

I know that Mongoose has some quirks when it comes to GeoJSON and I hope this is just another one.

Upvotes: 1

Views: 1665

Answers (1)

Jakeii
Jakeii

Reputation: 1273

Your geometry schema property should just be set to an array of Numbers to store the coordinates, like so:

geometry : {
    type: [Number],
    index: '2dsphere` //some geospatial queries require this
}

and then query and updated as such

{ $set: { geometry: data.geometry.coordinates } }

Upvotes: 2

Related Questions