Mongoose setDefaultsOnInsert and 2dsphere index not working
The setDefaultsOnInsert
option uses the $setOnInsert
operator to perform its function, and it looks like that's conflicting with your own use of $setOnInsert
to set the location
.
A workaround would be to remove the setDefaultsOnInsert
option and put it all in your own $setOnInsert
operator:
model.user.user.findOneAndUpdate( {facebookId: request.params.facebookId}, { $setOnInsert: { activated: false, creationDate: Date.now(), email: request.payload.email, location: { type: 'Point', coordinates: request.payload.location.coordinates } } }, {upsert: true, new: true}, function (err, user) { if (err) { console.log(err); return reply(boom.badRequest(authError)); } return reply(user); });
One of the devs responded to my issue on github and added it as a milestone for the 4.0.8 release, this was his workaround:
$setOnInsert: { facebookId: request.params.facebookId, email: request.payload.email, 'location.type': 'Point', 'location.coordinates': request.payload.location.coordinates }
His solution does NOT work for me, I get the following error:
{ [MongoError: exception: insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?: { _id: ObjectId('559024a0395dd599468e4f41'), facebookId: 1.020272578180189e+16, location: { coordinates: [], type: "Point" }, email: "druwe.jeroen@gmail.com", gender: "male", lastName: "Druwé", firstName: "Jeroen", __v: 0, activated: false, creationDate: new Date(1435509921264), familyDetails: { children: [] } }]