Update

Update( ref, param_object )
Update( ref, param_object )
Update( ref, param_object )
Update( ref, param_object )
q.update( ref param_object )
Update( ref param_object )

Description

The update operation only modifies the specified fields in the documents pointed to by ref. Updates are partial, and only modify values that are specified in the param_object. Changes to scalar values and arrays are entirely replaced by the new data. Modifications to objects are merged. Setting a value to null completely removes the value. Fields in the document not specified in the param_object are not modified.

For performance, Databases, Collections, Functions, Indexes, Keys, and Roles use a cache. When you use Update to modify any of these Fauna schema documents, the modification is not guaranteed to be visible immediately.

Parameters

Argument Type Definition and Requirements

ref

A reference identifying the document that should be modified.

param_object

An object containing the document data to update, as well as optional new credentials and delegates. See the following section for details.

param_object

Field Name Field Type Definition and Requirements

data

The document’s user data to be modified.

credentials

The document’s credentials to be modified.

delegates

The document’s delegates to be modified.

Returns

An object containing the metadata about the update operations.

Field Name Field Type Definition and Requirements

ref

The reference that identifies the document just updated.

data

A copy of the new document data.

ts

The timestamp, with microsecond resolution, of the document update.

Examples

The following query updates the document by changing the name field to the value "Mountains’s Thunder" and removing the cost field from the document. All other fields in the document remain unchanged.

client.Query(
  Update(
    Ref(Collection("spells"), "181388642581742080"),
    Obj("data", Obj("name", "Mountain's Thunder", "cost", Null()))
  )
);
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539727423,
  "data": { "name": "Mountain's Thunder", "element": "air" }
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "update": { "@ref": "classes/spells/181388642581742080" },
          "params": {
            "object": {
              "data": {
                "object": { "name": "Mountain's Thunder", "cost": null }
              }
            }
          }
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "ref": { "@ref": "classes/spells/181388642581742080" },
    "class": { "@ref": "classes/spells" },
    "ts": 1509244539727423,
    "data": { "name": "Mountain's Thunder", "element": "air" }
  }
}
result, _ := client.Query(
  f.Update(
      f.RefCollection(f.Collection("spells"), "181388642581742080"),
      f.Obj{
          "data": f.Obj{"name": "Mountain's Thunder", "cost": nil}}))

if (err != nil) {
  fmt.Println(err)
} else {
  fmt.Println(result)
}
map[data:map[element:air name:Mountain's Thunder] ref:{181388642581742080 0xc00009a1b0 0xc00009a1b0 <nil>} ts:1588113146710000]
System.out.println(
    client.query(
        Update(
           Ref(Collection("spells"), Value(181388642581742080L)),
           Obj(
              "data", Obj(
                  "name", Value("Mountain's Thunder"),
                  "cost", Null()
              )
           )
        )
    ).get());
{
   ref: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))),
   ts: 1526674566645179,
   data: {
     name: "Mountain's Thunder", 
     element: "air"
   }
 }
client.query(
  q.Update(
    q.Ref(q.Collection('spells'), '181388642581742080'),
    {
      data: {
        name: 'Mountain\'s Thunder',
        cost: null,
      },
    },
  )
)
.then((ret) => console.log(ret))
{ ref:
   Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))),
  ts: 1527276015058883,
  data: { name: 'Mountain\'s Thunder', element: [ 'air' ] } }
client.query(
  q.update(
    Ref(Collection("spells"), "181388642581742080"),
    {
      "data": {
        "name": "Mountain's Thunder",
        "cost": None
      }
    }
  )
)
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539727423,
  "data": { "name": "Mountain's Thunder", "element": "air" }
}
client.query(
  Update(
    Ref(Collection("spells"), "181388642581742080"),
    Obj(
      "data" -> Obj("name" -> "Mountain's Thunder", "cost" -> Null())
    )
  )
)
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539727423,
  "data": { "name": "Mountain's Thunder", "element": "air" }
}

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!