Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose.js except _id & __v from query result by default

Tags:

mongoose

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

like image 612
WHITECOLOR Avatar asked Feb 22 '13 08:02

WHITECOLOR


1 Answers

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.

like image 174
JohnnyHK Avatar answered Oct 23 '22 07:10

JohnnyHK