Developer
Developer

Reputation: 25

Update value in nested field in object - Firestore

I'm trying to update a nested field in Firestore and I'm somehow overriding the entire object. A map of my data is shown below: enter image description here

And I try to update rundate and status with this command:

 //Assume deptName is extrusion

    const item = await db.doc(`orders/${orderId}/batches/${batchNumber}`)
   .update({
     [`batchDeptStatusInfo.${deptName}`]: {
      status:"Scheduled",
      rundate: newRunDate,
    }
   }) 

However, this overwrites the entire object. As seen above, conversion has four properties, while extrusion (the property I tried to update) only has 2.

Can anyone provide some guidance on this issue? I've seen posts that say use dot notation and update not set and I think I check those boxes

Upvotes: 1

Views: 34

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

When you specify a new value for a field, the value you specify will replace the entire value for that field. The server doesn't perform any sort of patching between the existing value and what you specify.

If you want to perform granular updates, make sure to specify the entire path to where you want to set the value.

So:

await db.doc(`orders/${orderId}/batches/${batchNumber}`)
 .update({
   [`batchDeptStatusInfo.${deptName}.status`]: "Scheduled",
   [`batchDeptStatusInfo.${deptName}.rundate`]: newRunDate
 })

Upvotes: 1

Related Questions