Mongoose - which update function will also generate id for new property?

Viewed 14

I have a subdocument inside a dosument and when I update my document, I want to generate ObjectId's. Both update and findByIdAndUpdate dont do that. I actually have to manually do that in my controller. Is there any way i can make it work?

const productSchema = new mongoose.Schema({
   ...
   reports: [ReportSchema]
})

const ReportSchema = new mongoose.Schema({
   ...
   info: String,
   date: date
})

my controller:

updateProduct: async (req, res) => {
const product = await Product.update({id: id}, {
                $push: {
                    report: {
                        info: info,
                        date: new Date(),
                        //_id: mongoose.Types.ObjectId() // only this works
                    },
                }, $set: {isReport: true}}
            )
            res.status(200).send()
        }
}
0 Answers
Related