Description
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
const mongoose = require('mongoose');
const carSchema = require('./car');
const continentalWheelSchema = require('./wheel/continental');
const michelinWheelSchema = require('./wheel/michelin');const CarSchema = new mongoose.Schema({}, { discriminatorKey: 'type', _id: false } );
const WheelSchema = new mongoose.Schema({}, { discriminatorKey: 'type', _id: false } );const ClientParkSchema = new mongoose.Schema({
customer: { type: String, required: true },
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now },
cars: [CarSchema],
wheels: [WheelSchema]
});ClientParkSchema.path('cars').discriminator('break', carSchema);
ClientParkSchema.path('wheels').discriminator('continental', continentalWheelSchema);
ClientParkSchema.path('wheels').discriminator('michelin', michelinWheelSchema);module.exports = {
ClientPark: mongoose.model('client_park', ClientParkSchema, 'client_park')
};/////////////////////// CAR SCHEMA ////////////////////////
const mongoose = require('mongoose');
const CarSchema = new mongoose.Schema({
brand: { type: String, required: true },
model: { type: String, required: true },
}, { _id: false } );module.exports = CarSchema;
/////////////////////// CONTINENTAL WHEEL SCHEMA ////////////////////////
const mongoose = require('mongoose');
const ContinentalWheelSchema = new mongoose.Schema({
model: { type: String, required: true }
}, { _id: false } );module.exports = ContinentalWheelSchema;
/////////////////////// MICHELIN WHEEL SCHEMA ////////////////////////
const mongoose = require('mongoose');
const MichelinWheelSchema = new mongoose.Schema({
price: { type: number, required: true},
model: { type: String, required: true },
season: { type: String, required: true }
}, { _id: false } );module.exports = MichelinWheelSchema;
my mongoose query :
clientParkModels.ClientPark.updateOne(
{
'_id': '5d39a19e2376c7089ec7707b', 'car.type': 'break', 'wheel.type': 'continental'
},
{
$set: {
'cars.$.brand': 'Ford',
'wheels.$.model': 'X5485622',
'wheels.$.price': 10000,
'wheels.$.season': 'summer'
}
}
).exec();
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