9

I can except a field from query results declaring it like:

field: {type: 'string', select: false}

Is but it possible to do that with _id and __v field? I tried

_id: {select: false}

But It seems not to work

WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181

1 Answers1

17

You can do this as long as you also include the type of the field in the schema definitions:

_id: {type: mongoose.Schema.ObjectId, select: false},
__v: {type: Number, select: false},

However, that's going to prevent Mongoose from being able to find your model instance (and update its __v) on a save unless you explicitly include those fields in your find. So make sure you know what you're doing.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 1
    when use _id: { type: Schema.Types.ObjectId, select: false }, it works. It seems mongoosejs change the definition: http://mongoosejs.com/docs/schematypes.html – Jin Jan 16 '18 at 03:40