Discussion

Arbitrary reference in Mongoose schema

Supposing you need parent reference of Task schema, and parent can be either manager or developer. You don’t have to define both reference IDs in Task schema. Instead MongoDB provides feature to define arbitrary reference.

const taskSchema = new Schema({
  ...
  parent: [{
    _id: {
      type: String,
      refPath: 'parent.object'
    },
    object: String
  }],
});

You can assign value as Manager schema ID or Developer schema ID.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *