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

Given the following document:

{
  id: "392886847463751746",
  coll: Product,
  ts: Time("2099-04-10T16:50:12.850Z"),
  name: "limes",
  description: "Organic, 1 ct",
  price: 0.65
}

Call update() with an object containing updated document fields:

Product.byId("392886847463751746")?.update({
  name: "key limes",
  quantity: 100
})
{
  id: "392886847463751746",
  coll: Product,
  ts: Time("2024-04-10T17:57:36.480Z"),
  name: "key limes",
  description: "Organic, 1 ct",
  price: 0.65,
  quantity: 100
}

Remove a field

To remove a document field, set its value to null:

Product.byId("392886847463751746")?.update({
  quantity: null
})
{
  id: "394802471374946368",
  coll: Product,
  ts: Time("2024-04-10T17:59:50.970Z"),
  name: "key limes",
  description: "Organic, 1 ct",
  price: 0.65
}

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!