I have a document (product) with subdocuments (reports).
I am scraping and adding reports. Then I have a cron job and when the announcedRepair
date ‘arrives’ I want to do the following simultaneously:
- Delete all the reports with certain
announcedRepair
date - then right after add a custom report saying it’s fixed.
- if there are multiple reports with the same
announcedRepair
, don’t add multiple custom reports, but only one while adding, I’m trying to check that
Nothing I’ve tried so far works. either I got it to work so I delete and add custom reports, but then I can’t manage to only 1 custom fixed report, or when I add the condition, it’s not working at all (how I have it now, I’m adding the whole condition as a string to
db ('"$cond":[
{ "$eq": ["$isBroke", true]},...')
How can I do it? Thanks!!
const productSchema = new mongoose.Schema({
name: {
type: String
},
reports: [ReportSchema]
...
})
mongoose.model('Product', productSchema);
const reportSchema = new mongoose.Schema({
title: String,
category: String,
announcedRepair: Date,
isBroke: Boolean
...
});
const isFixedreport = {
_id: id,
name,
category,
announcedRepair: null,
isBroke: false,
...
};
await Product.findOneAndUpdate({
"reports.announcedRepair": announcedRepair
},
{
"$pull": {
"reports": {
"announcedRepair": announcedRepair
}
}
}, {new: true})
await Product.findOneAndUpdate({
"name": name,
"category": category
},
{
"$push": {
"reports": {
"$cond":[
{ "$eq": ["$isBroke", true]},
, isFixedreport,
"$$REMOVE"
]
}
}
}, {new: true})