document.updateData()
Update document data field contents.
Description
The updateData()
method updates the document data field with the
object fields and returns the updated document. During the update, object
fields are copied to the document data field, 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
.
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("392886847463751746")?.updateData({
id: "12345",
coll: "Product",
name: "key limes",
stock: 100
})
{
id: "392886847463751746",
coll: Product,
ts: Time("2099-04-10T17:02:43.846Z"),
name: "key limes",
description: "Conventional, 16 oz bag",
price: 299,
stock: 100,
category: Category("401610017107607625"),
data: {
coll: "Product",
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!