mongoose findone exclude perticular field 1

mongoose findone exclude perticular field

//Return the User document without having password field
User.findOne({_id: userId}).select("-password")

//Incase wants to add excluded field
User.findOne({_id: userId}).select("+password")

Here is what the above code is Doing:
1. We are using the findOne() method to find a user by their id.
2. We are using the select() method to exclude the password field from the returned document.
3. We are using the select() method to include the password field in the returned document.

Similar Posts