Automattic / Automattic/mongoose

Map of nested discriminators

Open
#8,361 6 comments 5 reactions 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

**Do you want to request a *feature* or report a *bug*?**
Feature

**What is the current behavior?**
It would be very useful to allow for a map of nested discriminators.
While you could make a case that an array of embedded discriminators is a better schema design, sometimes you have to work with a predetermined data structure.

**What is the expected behavior?**
```
const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' });
const schema = Schema({ shape: {type: Map, of: shapeSchema} });

// error
schema.path('shape').discriminator('Circle', Schema({ radius: String }));
schema.path('shape').discriminator('Square', Schema({ side: Number }));

const MyModel = mongoose.model('ShapeTest', schema);

let doc = new MyModel({ shape: {a: { kind: 'Circle', radius: 5 }, b: { kind: 'Square', side: 10 }} });
doc.shape.a.radius; // 5
doc.shape.b.side; // 10
```

**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**

Node.js 10.16.3
Mongoose 5.7.12
MongoDB 4.0.3

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the schema and document example from the issue with the stated Node.js, Mongoose, and MongoDB versions. Then trace how Map paths and discriminators are handled, comparing the requested nested behavior with embedded discriminators. Done means Circle and Square entries in separate map keys hydrate with their respective fields without the shown error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.