-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating document with array of discriminators #8063
Comments
You might be able to do this using array filters: https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-36-array-filters.html . If not, you should just use findOne() and update the document using save() |
This must be is a bug. You cannot update non-common fields on embedded array discriminators. I have the same issue. But if you try to update one of the common fields, it works. Example : const slideSchema = new Schema(
{
type: {
type: String,
required: true
},
commonField: String
},
{ discriminatorKey: 'type' }
);
const schema = new Schema({
....
slides: [slideSchema]
});
const slidesSchema = schema.path('slides');
slidesSchema.discriminator('typeA', new Schema({ a: String });
slidesSchema.discriminator('typeB', new Schema({b: string}); If I use something like : MyModel.findOneAndUpdate(
{
'slides._id': id // this is a typeA slide
},
{
a: 'test', // Will NOT be updated
commonField: 'newValue' // Will be updated
{
new: true,
runValidators: true
}
); #6087 seems to be related. |
@SebT what you reported is expected behavior, because Mongoose has no way of knowing that const mongoose = require('mongoose');
mongoose.set('debug', true);
mongoose.set('useFindAndModify', false);
const Schema = mongoose.Schema;
const slideSchema = new Schema(
{
type: {
type: String,
required: true
},
commonField: String
},
{ discriminatorKey: 'type' }
);
const schema = new Schema({
slides: [slideSchema]
});
const slidesSchema = schema.path('slides');
slidesSchema.discriminator('typeA', new Schema({ a: String }));
slidesSchema.discriminator('typeB', new Schema({b: String}));
const Slide = mongoose.model('Slide', schema);
const slidesSchema = schema.path('slides');
slidesSchema.discriminator('typeA', new Schema({ a: String }));
slidesSchema.discriminator('typeB', new Schema({b: String}));
const Slide = mongoose.model('Slide', schema);
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true });
const doc = await Slide.create({
slides: [{ type: 'typeA', commonField: 'foo', a: 'bar' }]
});
await Slide.findOneAndUpdate(
{
'slide':{ $elemMatch: { _id: doc.slides[0]._id, type: 'typeA' } } // <-- change here
},
{
'slides.$.a': 'test'
'slides.$.commonField': 'newValue'
},
{
new: true,
runValidators: true
}
);
} However, that doesn't seem to work. Will fix that 👍 |
Do you want to request a feature or report a bug?
It's not a bug or a feature, I just want an example for discriminators. Sorry about this post but I'm really stuck and the documentation didn't help me.
What is the current behavior?
It's not a behavior but more a request to understand how could I update a document contains 2 properties whichs are array of discriminators ? I didn't find any sample about that.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node : 10.16.1
MongoDB: 3.6.3
Mongoose: 5.6.9
my mongoose query :
My document is not updated. So, I would like if I can update 2 differents discriminator in the same query ?
Could you provide an example ?
Thank you very much
The text was updated successfully, but these errors were encountered: