H
H
HoHsi2016-03-03 18:05:23
MongoDB
HoHsi, 2016-03-03 18:05:23

How to hide _id and __v in mongoose?

Good afternoon!
How to hide _id and __v fields by default in Mongoose schema. (i.e. not just throw them out during transformation, but just make them select: false).
Tried like this:

const UserSchema = new Schema({
  _id: {
    type: mongoose.Schema.ObjectId,
    select: false
  },
  __v: {
    type: Number,
    select: false
  }
});

But in this case they are not generated by default. Tried through custom transformation, but in this case it doesn't work.select('+_id')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Yandimirkin, 2016-03-04
@vlaad360

No way :(
I just have each user has his own role, the roles contain which fields not to show.
You can make a separate static method that removes unnecessary keys from the object, but in any case, you have to put a crutch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question