say I wanted to update thousands of mongodb models with one click, would I run into unexpected problems? The idea is to update all entries as soon as there is a third language added to the webapp.
Model:
JobSchema = new mongoose.Schema({
info:{
instruments:[{
index:String,
de:String,
en:String,
}],
},
});
Custom Updating (thousands of results):
Job.find({}, function(err, foundJobs){
//no error handling for the sake of simplicity
foundJobs.forEach(function(job){
job.info.instruments.forEach(function(instr, index){
instr.en = 'FOO'
})
})
})
I know the better idea would be to create an extra "Instrument" model, but then I couldn't search for JobModels containing certain instruments...at least I couldn't find a good method...
Try this...
Job.updateMany({},
{ $set: { instruments.$.en: 'FOO' }, (err, result) => {
if (err) {
next(err);
} else {
res.status(200).json(result);
}
});
I know the better idea would be to create an extra "Instrument" model, but then I couldn't search for JobModels containing certain instruments...at least I couldn't find a good method...
you could search by Id's of a references array in Job.
JobSchema = new mongoose.Schema({
info:{
instruments: [{type: mongoose.Schema.Types.ObjectId, ref: 'Instrument'}],
}
});
InstrumentSchema = new mongoose.Schema({
index:String,
de:String,
en:String
});
// assuming instrumentIDs is an array containing instrument object IDs.
JobSchema.find({ info.instruments: {$all : instrumentIDs } })
imho, if the update you're describing happens once in a full moon, it's not worth normalizing over it.