I\'m using the Mongoose Library for accessing MongoDB with node.js
Is there a way to remove a key from a document? i.e. not just set the value to n
I use mongoose and using any of the above functions did me the requirement. The function compiles error free but the field would still remain.
user.set('key_to_delete', undefined, {strict: false} );
did the trick for me.
the problem with all of these answers is that they work for one field. for example let's say i want delete all fields from my Document if they were an empty string ""
.
First you should check if field is empty string put it to $unset
:
function unsetEmptyFields(updateData) {
const $unset = {};
Object.keys(updatedData).forEach((key) => {
if (!updatedData[key]) {
$unset[key] = 1;
delete updatedData[key];
}
});
updatedData.$unset = $unset;
if (isEmpty(updatedData.$unset)) { delete updatedData.$unset; }
return updatedData;
}
function updateUserModel(data){
const updatedData = UnsetEmptyFiled(data);
const Id = "";
User.findOneAndUpdate(
{ _id: Id },
updatedData, { new: true },
);
}
In early versions, you would have needed to drop down the node-mongodb-native driver. Each model has a collection object that contains all the methods that node-mongodb-native offers. So you can do the action in question by this:
User.collection.update({_id: user._id}, {$unset: {field: 1 }});
Since version 2.0 you can do:
User.update({_id: user._id}, {$unset: {field: 1 }}, callback);
And since version 2.4, if you have an instance of a model already you can do:
doc.field = undefined;
doc.save(callback);
I'm trying to remove a field called 'cover' from my document but it is of type buffer. so for some reason even if I make it undefined it still remains binary type while giving me undefined result this route returns the cover field from document matching profile property
`
exports.update = (req, res) => {
let form = new formidable.IncomingForm();
form.keepExtension = true;
form.parse(req, (err, fields, files) => {
if (err) {
return res.status(400).json({
error: "Photo could not be uploaded",
});
}
let user = req.profile;
user = _.extend(user, fields);
if (fields.password && fields.password.length < 6) {
return res.status(400).json({
error: "Password should be min 6 characters long",
});
}
if (fields.cover == null) {
user.cover = undefined;
}
if (files.photo) {
if (files.photo.size > 10000000) {
return res.status(400).json({
error: "Image should be less than 1mb",
});
}
sharp(files.photo.path)
.resize(300, 300, {
fit: "cover",
})
.jpeg({
quality: 90,
})
.toFile(`assets/users/${user.profile}.jpg`, (err, info) => {
if (err) {
return res.status(400).json({
error: "unable to process the image",
});
}
});
}
if (files.cover) {
if (files.cover.size > 10000000) {
return res.status(400).json({
error: "Image should be less than 1mb",
});
}
sharp(files.cover.path)
.resize(1200, 675, {
fit: "cover",
})
.jpeg({
quality: 90,
})
.toBuffer((err, compressed, info) => {
User.findOneAndUpdate(
{ profile: user.profile },
{ cover: compressed }
)
.select("_id")
.exec((err, updatedUser) => {
if (err)
res.status(400).json({
error: "Unable to process the image",
});
});
});
}
console.log("user", user);
user.save((err, result) => {
if (err) {
return res.status(400).json({
error: errorHandler(err),
});
}
user.hashed_password = undefined;
user.salt = undefined;
user.photo = undefined;
res.json(user);
console.log("result", result);
});
});
};
`
you can use delete user._doc.key