I am trying to add a new field to a document, but this isn't working:
Creating my UserModel prototype:
model = require("../models/user")
UserModel.prototype.findOneAndUpdate = function(query, params, cb) {
model.findOneAndUpdate(query, params, { returnNewDocument: true, new: true }, function(err, data) {
if (!err) {
cb(false, data);
} else {
cb(err, false);
}
});
};
Then calling it
userFunc = require("../../model_functions/user")
userFunc.findOneAndUpdate({
"email.value": userEmail
}, {
$set: {"wat":"tf"}
},
function (err, updatedUser) {
//This logs the updated user just fine, but the new field is missing
console.log(updatedUser);
...
});
This successfully updates any field as long as it exists, but it won't add any new one.
wat
defined within the schema for users? Mongoose is designed to enforce the schema, including discarding properties that don't align to it. – ToolMixed
type that can then contain any variety of information needed. Otherwise, if you don't want the schema enforced like this, then Mongoose probably isn't right for your use case. – Tool