I have Category mongoose document and I want to update his products.Every time on update products just rewrite them and I have only the last put record.How can I populate products?
const category = new mongoose.Schema({
products: [{ type: mongoose.Schema.Types.ObjectId, ref: "Product" }],
});
module.exports = mongoose.model("Category", category);
exports.updateCategory = catchAsyncErrors(async (req, res, next) => {
let category = await Category.findById(req.params.id);
category = await Category.findByIdAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true,
useFindAndModify: false,
})
res.send(200).json({
success: true,
category
});
});