cfibla cfibla - 6 days ago 4x
Node.js Question

How can I delete an item from an Object in NodeJs/express/mongoose?

I know this has been asked before, but no one of the answers worked for me.
In my App, I have a users collection in MongoDb. These users collection have an array field named 'segActuacions' that can be modified within a form. This form sends this Object to router:

"segActuacions.0.body":"first item",
"segActuacions.1.body":"second item"}

router has this line to go to controller:


where ':id' is User-id, and ':i' is the index of segActuacions

controller has this code:

exports.actuaDelete = function (req, res) {

var userId =;
var userI = req.params.i;
var usr = req.body;

delete usr[''];
delete usr['segActuacions.userI.body'];

models.User.findByIdAndUpdate(userId, usr,

function (error, user){
if (error) res.json(error);

I want to delete the fields 'i' of the Object (for instance: if i=0, i'd like to delete "" and "segActuacions.0.body", ...but nothing happens

Thanks for your answer


Note your delete wouldn't work because you are using userI as a string and not using the variable value. Also update will just update the fields that are in the object.

But I think you should use $unset, like the following:

var updateObj = {};
updateObj['segActuacions'] = {};
updateObj['segActuacions'][userI] = {};
updateObj['segActuacions'][userI]['body'] = '';
updateObj['segActuacions'][userI]['date'] = '';

var update = { $unset:  updateObj };

models.User.findByIdAndUpdate(userId, update, function (error, user){
    if (error) res.json(error);

(This code can be improved and wasn't tested)