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

159
Views
Patch (findByIdAndUpdate) in Mongoose

I have to do a simple CRUD in Node/Mongoose API. I also have to make put and patch and while doing patch, I'm a bit confused, because I wanted to use findByIdAndUpdate method, but as I see on examples, there is only one record updated, e.g. {name: "newName"}. I was wondering what I should do if for example I wanted to update two cells?

My schema:

const userSchema = new Schema({
    login: String,
    email: String,
    registrationDate: Date,
});

My Patch code:

router.patch('/:id', async (req, res) => {
    const id = req.params.id;
    User.findByIdAndUpdate(id, { ??? },
        function (err, docs) {
            if (err){
                console.log(err)
            }
            else{
                console.log("Updated User : ", docs);
            }
        });

});

I don't know what I should write in "???", because what if I wanted to update only the login, and what if I wanted to update name and email? Maybe I'm wrong and PATCH is used only to edit ONE cell and in other cases I should use PUT?

Edit:

I made it work using something like this:

router.patch('/:id', async (req, res) => {
    const id = req.params.id;
    let updates={}
    if (req.body.login) {
        updates["login"] = req.body.login
    }
    if (req.body.email) {
        updates["email"] = req.body.email
    }
    if (req.body.registrationDate) {
        updates["registrationDate"] = req.body.registrationDate
    }
    User.findByIdAndUpdate(id, updates,
        function (err, docs) {
            if (err){
                console.log(err)
            }
            else{
                console.log("Updated User : ", docs);
            }
        });

});

Anyway I have a question what I should do to "stop" the action. I'm using HTTPie and when I write http PATCH......, it seems like I can't write anything else, because it's still working, I need to do CTRL+C to stop the query.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just write the fields you want to change and the new values ​​separated by commas.

const id= req.params.id;
const email = "newemail";
const date = "1991/13/01";
const user = await User.findByIdAndUpdate(
    {
      _id: id,
    },
    { email, registrationDate: date},
    { upsert: false }
  );
about 4 years ago · Juan Pablo Isaza Report
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!