How to merge two Mongoose Arrays inside Document in findByIdAndUpdate method Typescript Express

Viewed 77

Have this model

export const IEventSchema = new mongoose.Schema<IEventBase>({
    type: {type: String, enum: EventType, required: true},
    date: {type: Number, required: true},
    title: {type: String},
    description: {type: String},
    notifiedUsers: [
        {
            type: Schema.Types.ObjectId,
            ref: 'User',
            default: {}
        }
    ],
    unnotifiedUsers: [
        {
            type: Schema.Types.ObjectId,
            ref: 'User',
            default: {}
        }
    ],
});

I want to move the contents of the notifiedUsers array to unnotifiedUsers, inside the findByIdAndUpdate method of the router.

router.put('/delay/:id', async (req: Request, res: Response) => {
    try {
        await Event.findByIdAndUpdate(req.params.id, {
                $set: {date: req.body.date},
                // Write Here
            }
        );
        res.status(200).json(`Event delayed to ${req.body.date}`);

    } catch (e: any) {
        console.log(e);
    }
});
$addToSet: {unnotifiedUsers: {$each: 'notifiedUsers'}} // Does not working
0 Answers
Related