Skip to content

Commit 426cc8c

Browse files
authored
Merge pull request #12861 from Automattic/vkarpov15/gh-6087
docs(discriminators): add section about changing discriminator key
2 parents b7816cd + db81dbb commit 426cc8c

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

docs/discriminators.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,25 @@ instances.
3131

3232
### Discriminator keys
3333

34-
The way mongoose tells the difference between the different
35-
discriminator models is by the 'discriminator key', which is
36-
`__t` by default. Mongoose adds a String path called `__t`
37-
to your schemas that it uses to track which discriminator
38-
this document is an instance of.
34+
The way Mongoose tells the difference between the different discriminator models is by the 'discriminator key', which is `__t` by default.
35+
Mongoose adds a String path called `__t` to your schemas that it uses to track which discriminator this document is an instance of.
3936

4037
```javascript
4138
[require:Discriminator keys]
4239
```
40+
41+
### Updating the discriminator key
42+
43+
By default, Mongoose doesn't let you update the discriminator key.
44+
`save()` will throw an error if you attempt to update the discriminator key.
45+
And `findOneAndUpdate()`, `updateOne()`, etc. will strip out discriminator key updates.
46+
47+
```javascript
48+
[require:Update discriminator key]
49+
```
50+
51+
To update a document's discriminator key, use `findOneAndUpdate()` or `updateOne()` with the `overwriteDiscriminatorKey` option set as follows.
52+
53+
```javascript
54+
[require:use overwriteDiscriminatorKey to change discriminator key]
55+
```

test/docs/discriminators.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,42 @@ describe('discriminator docs', function() {
100100
assert.equal(event3.__t, 'SignedUp');
101101
});
102102

103+
it('Update discriminator key', async function() {
104+
let event = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
105+
await event.save();
106+
107+
event.__t = 'SignedUp';
108+
// ValidationError: ClickedLink validation failed: __t: Cast to String failed for value "SignedUp" (type string) at path "__t"
109+
// acquit:ignore:start
110+
await assert.rejects(async () => {
111+
// acquit:ignore:end
112+
await event.save();
113+
// acquit:ignore:start
114+
}, /__t: Cast to String failed/);
115+
// acquit:ignore:end
116+
117+
event = await ClickedLinkEvent.findByIdAndUpdate(event._id, { __t: 'SignedUp' }, { new: true });
118+
event.__t; // 'ClickedLink', update was a no-op
119+
// acquit:ignore:start
120+
assert.equal(event.__t, 'ClickedLink');
121+
// acquit:ignore:end
122+
});
123+
124+
it('use overwriteDiscriminatorKey to change discriminator key', async function() {
125+
let event = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
126+
await event.save();
127+
128+
event = await ClickedLinkEvent.findByIdAndUpdate(
129+
event._id,
130+
{ __t: 'SignedUp' },
131+
{ overwriteDiscriminatorKey: true, new: true }
132+
);
133+
event.__t; // 'SignedUp', updated discriminator key
134+
// acquit:ignore:start
135+
assert.equal(event.__t, 'SignedUp');
136+
// acquit:ignore:end
137+
});
138+
103139
/**
104140
* Discriminator models are special; they attach the discriminator key
105141
* to queries. In other words, `find()`, `count()`, `aggregate()`, etc.

0 commit comments

Comments
 (0)