I have this Mongoose schema:
const User = mongoose.model('User', new Schema({
    id: String,
    name: String,
    extra: { bb: Number, chain: Number }
}), 'users');
When I see a new User, it's stored like this:
{ 
  _id: ...,
  id: '1234',
  name: 'John',
  extra: {
    _id: ...,
    bb: 54,
    chain: 7
  },
  __v: 1
}
As you see, Mongoose (or Mongo, I don't know) is including a _id in embedded object extra. Why is that happening? How can I prevent it to happen?
You can declare "_id: false" for a subschema as shown below.
const User = mongoose.model('User', new Schema({
     id: String,
     name: String,
     extra: { bb: Number, chain: Number, _id: false }
}), 'users');
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With