update()

Update document fields.

Signature

update(object: { ttl: Time | Null, *: Any }): <Document>

Description

The update() method updates the document with the object fields and returns the updated document. During the update, fields from object are copied to the document, creating new fields or updating existing fields. The operation is similar to a merge.

Only the fields included in object are updated, all other document fields aren’t updated. Fields with nested objects in object are merged with the identically named nested object in the document.

To remove a document field, set its value in object to null. Deleted fields aren’t saved.

Parameters

Parameter Type Required Description

object

Object

Yes

Object with the updated document fields.

object fields

Name Type Required Description

ttl

Time

Timestamp indicating when to remove the document. When the document is removed, it ceases to exist and temporal queries can’t recover the document.
Default = null, which leaves the ttl unchanged.

Return value

Type Description

Document

Document with updated fields.

Examples

  1. Create and update a document:

    Character.create({
      name: "Red Shirt",
      phaser: "stun",
      termination: "35 seconds after teleport",
      coordinates: { x: 53, y: 17 },
    })!.update({
      termination: "56 seconds after teleport",
    })
    {
      id: "346368030577525248",
      coll: Character,
      ts: Time("2022-10-24T03:12:24.820Z"),
      name: "Red Shirt",
      phaser: "stun",
      termination: "56 seconds after teleport",
      coordinates: { "x": 53, "y": 17 }
    }
  2. Update the same document, changing fields in a nested object and removing a field:

    Character.firstWhere(.name == "Red Shirt")!.update({
      coordinates: { x: 4, z: 3 },
      phaser: null,
    })
    {
      id: "346368030577525248",
      coll: Character,
      ts: Time("2022-10-24T03:12:24.820Z"),
      name: "Red Shirt",
      termination: "56 seconds after teleport",
      coordinates: { "x": 4, "y": 17, "z": 3 }
    }

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!