V
V
Vladimir Mirka2017-11-20 17:29:26
MongoDB
Vladimir Mirka, 2017-11-20 17:29:26

MongoDB/Mongoose how to remove property from returned object?

I'm doing an update UserModel.findOneAndUpdate(_id, data). The whole object with new data is returned back.
How to say base. so that it does not include a certain property of the object?
For example, here I can control it:
UserModel.findOne({_id}, {property: 0})
Can I do the same here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yevhen Kravets, 2017-11-30
@krava7

you can pass an object with fields that you need or don't need further to the select method

UserModel.findOneAndUpdate(_id, data)
    .select({property: 0})

if you work through callbacks, then you call the exec method, which accepts it
UserModel.findOneAndUpdate(_id, data)
    .select({property: 0})
    .exec(function(err, user) {
        //...
    });

and if through promises - just call then
UserModel.findOneAndUpdate(_id, data)
    .select({property: 0})
    .then(user => {
        //...
    })
    .catch(err => {
        //...
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question