Automattic / Automattic/mongoose
Support updates that filter on multiple discriminator keys with `$in`
- 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*?**
Bug
**What is the current behavior?**
Using updateOne() on discriminated models properties does not work. Here is a repro script.
**If the current behavior is a bug, please provide the steps to reproduce.**
```javascript
const testSchema = new mongoose.Schema(
{
title: { type: String, required: true },
subtitle: String,
kind: { type: String, required: true }
},
{ timestamps: true, discriminatorKey: 'kind' }
);
const Test = mongoose.model('Test', testSchema);
const testSchemaChild = new mongoose.Schema({
label: String
});
const TestChild = Test.discriminator('TestChild', testSchemaChild, 'testchild');
(async () => {
await mongoose.dropDatabase();
try {
await Test.create({
title: 'Title 1',
subtitle: 'subtitle 1',
label: 'label 1',
kind: 'testchild'
});
await Test.create({
title: 'Title 2',
subtitle: 'subtitle 2',
kind: 'testchild'
});
let test = await Test.find();
console.log(JSON.stringify(test, null, 4));
const doc = await Test.updateOne(
{ label: 'label 1' },
{ label: 'NEW LABEL' }
);
console.log(JSON.stringify(doc, null, 4));
test = await Test.find();
console.log(JSON.stringify(test, null, 4));
} catch (error) {
console.log(`${error}`);
}
console.log(`END`);
process.exit(0);
})();
```
**Output**
```json
[
{
"kind": "testchild",
"_id": "5ceb9e2c5449f12f0707231f",
"title": "Title 1",
"subtitle": "subtitle 1",
"label": "label 1",
"createdAt": "2019-05-27T08:22:04.478Z",
"updatedAt": "2019-05-27T08:22:04.478Z",
"__v": 0
},
{
"kind": "testchild",
"_id": "5ceb9e2c5449f12f07072320",
"title": "Title 2",
"subtitle": "subtitle 2",
"createdAt": "2019-05-27T08:22:04.498Z",
"updatedAt": "2019-05-27T08:22:04.498Z",
"__v": 0
}
]
{
"n": 1,
"nModified": 1,
"ok": 1
}
[
{
"kind": "testchild",
"_id": "5ceb9e2c5449f12f0707231f",
"title": "Title 1",
"subtitle": "subtitle 1",
"label": "label 1",
"createdAt": "2019-05-27T08:22:04.478Z",
"updatedAt": "2019-05-27T08:22:04.509Z",
"__v": 0
},
{
"kind": "testchild",
"_id": "5ceb9e2c5449f12f07072320",
"title": "Title 2",
"subtitle": "subtitle 2",
"createdAt": "2019-05-27T08:22:04.498Z",
"updatedAt": "2019-05-27T08:22:04.498Z",
"__v": 0
}
]
END
```
**What is the expected behavior?**
I expected it to update label to the new label. Note that if you change the update to e.g. this, it works:
```javascript
const doc = await Test.updateOne(
{ title: 'Title 1' },
{ title: 'NEW' }
);
```
**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
Moongose 5.5.2
Mongodb 3.6.3
Node.js 10.13.0
Contributor guide
Research direction
Start with the provided JavaScript reproduction of Mongoose's Test.discriminator() and Test.updateOne() behavior, comparing the label filter with the working title filter. Trace how discriminator-key filters are handled during updateOne(), then add coverage for the reported multi-key/$in case and confirm that the matching document's label changes to the new value.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100