Replace

Replace( ref, param_object )
Replace( ref, param_object )
Replace( ref, param_object )
Replace( ref, param_object )
replace( ref, param_object )
Replace( ref, param_object )

Description

The replace operation substitutes the user data pointed to by the reference with the data contained in the param_object. Values not specified in the param_object are removed.

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

Parameters

Argument Type Definition and Requirements

ref

A reference type containing the document that should be modified.

param_object

An object that containing the document’s user data to be modified. See the following section for details.

param_object

Field Name Field Type Definition and Requirements

data

The document’s user data to be modified.

Returns

An object containing the metadata about the replace 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:

client.Query(
  Replace(
    Ref(Collection("spells"), "181388642581742080"),
    Obj(
      "data", Obj(
        "name", "Mountain's Thunder",
        "element", Arr("air", "earth")
      )
    )
  )
);
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539764856,
  "data": {
    "name": "Mountain's Thunder",
    "element": [ "air", "earth" ]
  }
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "replace": { "@ref": "classes/spells/181388642581742080" },
          "params": {
            "object": {
              "data": {
                "object": {
                  "name": "Mountain's Thunder",
                  "element": [ "air", "earth" ]
                }
              }
            }
          }
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "ref": { "@ref": "classes/spells/181388642581742080" },
    "class": { "@ref": "classes/spells" },
    "ts": 1509244539764856,
    "data": {
      "name": "Mountain's Thunder",
      "element": [ "air", "earth" ]
    }
  }
}
result, _ := client.Query(
    f.Replace(
        f.RefCollection(f.Collection("spells"), "181388642581742080"),
        f.Obj{
            "data": f.Obj{
                "name": "Mountain's Thunder",
                "element": f.Arr{"air", "earth"}
            },
        },
    ),
)

fmt.Println(result)
map[ref:{181388642581742080 0xc420237480 <nil>} ts:1509244539764856 data:map[element:[air earth] name:Mountain's Thunder]]
System.out.println(
    client.query(
        Replace(
            Ref(Collection("spells"), Value(181388642581742080L)),
            Obj(
               "data", Obj(
                          "name", Value("Mountain's Thunder"),
                           "element", Arr(Value("air"), Value("earth"))
                        )
             )
          )
    ).get());
{
  ref: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))),
  ts: 1526674566665295,
  data: {
    name: "Mountain's Thunder",
    element: ["air", "earth"],
  }
}
client.query(
  q.Replace(
    q.Ref(q.Collection('spells'), '181388642581742080'),
    {
      data: {
        name: 'Mountain\'s Thunder',
        element: ['air', 'earth'],
      },
    },
  )
)
.then((ret) => console.log(ret))
{ ref:
   Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))),
  ts: 1527275958680623,
  data:
   { name: 'Mountain\'s Thunder',
     element: [ 'air', 'earth' ]
   }
}
client.query(
  q.replace(
    q.ref(q.collection("spells"), "181388642581742080"),
    {
      "data": {
        "name": "Mountain's Thunder",
        "element": ["air", "earth"]
      }
    }
  )
)
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539764856,
  "data": {
    "name": "Mountain's Thunder",
    "element": [ "air", "earth" ]
  }
}
client.query(
  Replace(
    Ref(Collection("spells"), "181388642581742080"),
    Obj(
      "data" -> Obj(
        "name" -> "Mountain's Thunder",
        "element" -> Arr("air", "earth")
      )
    )
  )
)
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539764856,
  "data": {
    "name": "Mountain's Thunder",
    "element": [ "air", "earth" ]
  }
}

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!