question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schema plugins not applied to subdocuments

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
vkarpov15commented, Oct 4, 2016

@Jeff-Lewis in 4.6.3 schemas created implicitly for document arrays will have a $implicitlyCreated field.

1reaction
vkarpov15commented, Aug 22, 2016

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found