We have the following schema:
const DocumentSchema = new mongoose.Schema({
...
account: { type: mongoose.Schema.Types.ObjectId, ref: 'Accounts', required: true, index: true }
items: [SubDocumentSchema]
}, { timestamps: true })
DocumentSchema.plugin(mongooseIdValidator)
const SubDocumentSchema = new mongoose.Schema({
...
user: { type: mongoose.Schema.Types.ObjectId, ref: 'Users' }
product: { type: mongoose.Schema.Types.ObjectId, ref: 'Products' }
}, { timestamps: true })
As you can see, we try to verify the references in the DocumentSchema but NOT in the SubDocumentSchema, thus we set the plugin only on DocumentSchema. Based on the business requirements, we need to store a subdocument having a product id no matter that product id is valid or not (might not exist).
However, as we apply the plugin on DocumentSchema (to make sure the account field is valid), it also verifies all references in SubDocumentSchema, which we really do NOT want and for that reason we have NOT set the plugin on SubDocumentSchema.
Any way to stop propagation ?
We have the following schema:
As you can see, we try to verify the references in the
DocumentSchemabut NOT in theSubDocumentSchema, thus we set the plugin only onDocumentSchema. Based on the business requirements, we need to store a subdocument having a product id no matter that product id is valid or not (might not exist).However, as we apply the plugin on
DocumentSchema(to make sure theaccountfield is valid), it also verifies all references inSubDocumentSchema, which we really do NOT want and for that reason we have NOT set the plugin onSubDocumentSchema.Any way to stop propagation ?