Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now in beta.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

document.updateData()

Learn: Documents

Update a collection document using an object that may contain metadata fields.

Signature

updateData(data: { *: Any }) => <Document>

Description

Updates a collection document with fields from a provided data object.

During the update, fields from the data object are copied to the document, creating new fields or updating existing fields. The operation is similar to a merge.

This method differs from document.update() in how it handles reserved fields. See Reserved fields.

Nested fields

Fields with nested objects in the data object are merged with the identically named nested object in the document.

Remove a field

To remove a document field, set its value in the data object to null.

Default values

A field definition can set a default field value for documents in a collection. Default values are not inserted for missing or null fields during a document update.

Reserved fields

You can’t use this method to insert or edit the following metadata fields:

  • id

  • coll

  • ts

  • data

If the provided data object contains field names that conflict with these metadata fields, the method safely nests values for fields with reserved names in the data field. See Examples.

Parameters

Parameter Type Required Description

data

Object

true

Object with the updated document fields.

If the object contains field names that conflict with these metadata fields, the method safely nests values for fields with reserved names in the data field. See Examples.

Return value

Type Description

<Document>

The updated collection document.

A document’s data type is taken from its collection’s name. For example, Product for a document in the Product collection. See Document type.

Examples

Given the following document:

{
  id: "392886847463751746",
  coll: Product,
  ts: Time("2099-04-10T16:50:12.850Z"),
  name: "limes",
  description: "Conventional, 16 oz bag",
  price: 299,
  stock: 30,
  category: Category("401610017107607625")
}

Call updateData() with the id and coll fields in the object. These fields have the same name as reserved metadata fields.

Product.byId("777")?.updateData({
  id: "12345",
  coll: "Products",
  name: "key limes",
  stock: 100
})
{
  id: "777",
  coll: Product,
  ts: Time("2099-04-10T17:02:43.846Z"),
  cart: null,
  orders: "hdW...",
  name: "key limes",
  description: "Conventional, 16 oz bag",
  price: 299,
  stock: 100,
  category: Category("789"),
  data: {
    coll: "Products",
    id: "12345"
  }
}

Rather than return an error, updateData() treats any field with a reserved name as a document field and nests it in the document’s data property.

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!