Sometimes, we want to stop Mongoose from creating _id property for sub-document array items.
In this article, we’ll look at how to stop Mongoose from creating _id property for sub-document array items.
How to stop Mongoose from creating _id property for sub-document array items?
To stop Mongoose from creating _id property for sub-document array items, we can call mongoose.Schema with _id set to false.
For instance, we write
const mongoose = require("mongoose");
const subSchema = mongoose.Schema({}, {
_id: false
});
const schema = mongoose.Schema({
subSchemaCollection: [subSchema]
});
const model = mongoose.model('tablename', schema);
to create subSchema by calling mongoose.Schema with an object that has _id set to false.
Then we put the subSchema in the parent schema in the array we set as the value of subSchemaCollection.
Conclusion
To stop Mongoose from creating _id property for sub-document array items, we can call mongoose.Schema with _id set to false.