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

1

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

Schriever answered 22/2, 2013 at 8:56 Comment(0)
S
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.

Sena answered 22/2, 2013 at 13:40 Comment(1)
when use _id: { type: Schema.Types.ObjectId, select: false }, it works. It seems mongoosejs change the definition: mongoosejs.com/docs/schematypes.htmlMiletus

© 2022 - 2024 — McMap. All rights reserved.