Skip to content
Advertisement

How to exclude fields on return in mongoose

I have the following code:

JavaScript

It works perfectly but say I want to exclude a field returned, like the __v field. I know I can do this by just creating an object like

JavaScript

and etc. for teh other fields, however if I have 20 fields I don’t want to list every single one, so is there a way for mongoose to exclude a field when it returns after it’s created.

Advertisement

Answer

JavaScript

So, where b and c are the keys of the key/value pairs you want left out:

JavaScript

You might have to check the brackets there but I think that’s right.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement