Schema plugins not applied to subdocuments
See original GitHub issueI have the following Mongoose plugin to handle some common JSON conversion:
'use strict';
/**
* Default toJSON implementation for mongoose schema's
*/
module.exports = function toJsonPlugin(schema) {
//NOTE: this plugin is actually called *after* any schema's
//custom toJSON has been defined, so we need to ensure not to
//overwrite it. Hence, we remember it here and call it later
let transform;
if (schema.options.toJSON && schema.options.toJSON.transform) {
transform = schema.options.toJSON.transform;
}
//Set toJSON handler
schema.options.toJSON = {
transform(doc, ret) {
//Delete version and set ID
delete ret.__v;
if (ret._id) {
ret.id = ret._id.toString();
delete ret._id;
}
//Call custom transform if present
if (transform) {
transform(doc, ret);
}
}
};
};
This works great, and is applied on all my models. However, sub document schema’s don’t seem to inherit the behaviour for some reason.
For example, I have:
let NoticeSchema = new Schema({
title: String,
type: String,
message: String,
startDate: Date,
endDate: Date,
isEnabled: Boolean
});
This is a schema for a sub document, e.g.:
let ClubSchema = new Schema({
//...
notices: [NoticeSchema],
//...
});
However, the outputted JSON for the club includes _id
’s for the notices, and I have to explicitly define a toJSON
handler on the notice schema for it to work as intended:
NoticeSchema.options.toJSON = {
transform(doc, ret) {
ret.id = ret._id.toString();
delete ret._id;
}
};
Is this intentional or a bug? I would assume plugins for schema’s apply to all schema’s, also sub document schema’s.
This is with 4.5.0.
Issue Analytics
- State:
- Created 7 years ago
- Comments:21 (8 by maintainers)
Top Results From Across the Web
Mongoose v6.8.1: SubDocuments
Subdocument paths are undefined by default, and Mongoose does not apply subdocument defaults unless you set the subdocument path to a non-nullish value....
Read more >node.js - Sub documents inheritance in mongoose and ...
I have tried to implement inheritance for mongoDB sub document using mongoose-schema-extend, and later with mongoose itself.
Read more >Need help accessing a subdocument in a ... - MongoDB
Hello, I am working with Nodejs and Mongodb, particularly Mongoose. I have THREE schemas. The main schema creates the username/pass and has ...
Read more >mongoose-encryption - npm
You can even encrypt fields of sub-documents, you just need to add the encrypt plugin to the subdocument schema. Subdocuments are not ...
Read more >@prop | typegoose
@prop is used to mark properties to be in the Schema and also set Options of that property. note. Any Property that does...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Jeff-Lewis in 4.6.3 schemas created implicitly for document arrays will have a
$implicitlyCreated
field.@Jeff-Lewis I think mongoose only creates a new schema implicitly for document arrays. We can definitely mark those schemas with a property to make it easier to filter those out