How to exclude one particular field from a collection in Mongoose and JavaScript?

Estimated read time 1 min read

In Mongoose, you can use the select method to exclude a particular field from a query result. The select method takes a space-separated string of field names, with a leading minus sign (-) to exclude the specified fields.

Here’s an example:

const mongoose = require("mongoose");
const User = mongoose.model("User", new mongoose.Schema({
  name: String,
  email: String,
  password: String

User.find({}).select("-password").exec((error, users) => {

In this example, the query will return all the documents in the User collection, but exclude the password field from the result.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply