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

96
Views
Patch method is overwriting the whole document instead of replacing only changed field

I have a PATCH method for my API to update DB content in MongoDB. The DB schema has a nested structure and updating using the below method is replacing the whole set where few fields are removed.

The DB structure looks like

    {
    _id  : ObjectId(1234...),
    name : "name",
    age  : 30,
    levelone : [{ 
        "levelone": [{
            "name": "name1",
            "status": 1
        }, {
            "name": "name2",
            "status": 2
        }, {
            "name": "name3",
            "status": 1
        }],
        "levelone": "one",
        "levelonename": "abcdefgh" 
    },
    { 
        "leveltwo": [{
            "name": "name1",
            "status": 1
        }, {
            "name": "name2",
            "status": 2
        }],
        "levelone": "two",
        "levelonename": "asdadadasd" 
    }]
}

the update document structure looks like

{
    _id  : ObjectId(1234...),
    name : "name",
    age  : 30,
    levelone : [{ 
        "levelone": [{
            "name": "name1",
            "status": 1
        }, {
            "name": "name2",
            "status": 2
        }, {
            "name": "name3",
            "status": 1
        }] },
    { 
        "leveltwo": [{
            "name": "name1",
            "status": 1
        }, {
            "name": "name2",
            "status": 2
        }] 
    }]
}

The node js MongoDB query looks like

findByIdAndUpdate({'_id': id},{$set: updateObject});

the query removes the values levelone and levelonename and replaces the levelone.levelone, i want just to update the values of object levelone alone. Please advise

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!