Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!