update nested schema express update nested schema express mongoose mongoose

update nested schema express


I tried both of your ideas, but unfortunately those are not worked. I found a solution, maybe it is not elegant, but works:I changed my schema to:

var placeSchema = new Schema({  _id:  String,  zip: Number,  loc: {    type: Object  ,   index: '2dsphere'  }});

And my new code is:

if (typeof req.body.coordinates !== 'undefined') {                            place.loc = { type: 'Point', coordinates: req.body.coordinates };                    }


You need to tell Mongoose what the coordinates array contains if you want it to track changes for you. So change you schema definition to:

var placeSchema = new Schema({   _id:  String,  zip: Number,  loc: {    type: { type: String },    coordinates: [Number]  }});

The alternative would be to explicitly mark coordinates as changed:

place.coordinates = req.body.coordinates;place.markModified('coordinates');