FQL - Update() - Replace Child

Hello,

As the title describes, I am using the update method to patch a record.

In the record, I have a key string dictionary as a data field. Running an update on the record, will not remove cleared keys, only add new ones.

I understand this is where Replace would come into play, however, I don’t want to send the entire payload.

Are there any other options? Possible a flag to say, X child property is to be fully replaced, from within the update method?

Thank you in advance.

Hi @Harley-Torrisi and welcome! :wave:

You can remove a single key by assigning null to it. For example,

Update(Ref(Collection("thing"), "101"), {
  data: {
    add_new_field: 42,
    remove_field: null
  }
})

If you need to fully replace a child key, then you can execute in multiple steps, first clearing the entire value, then updating it with a new value.

Let(
  {
    ref: Ref(Collection("thing"), "101"),
    with_cleared_value: Update(Var("ref", {  data: {  key: null } })),
    with_new_value: Update(Var("ref", {  data: {  key: { /* ... */ } } })),
  },
  Var("with_new_value")
})
1 Like

Thanks mate, the second option seems to be the go to solution.

I have not used Let() yet, just been working with the “SQL” style methods. Does this example count as an additional read op?

I’m wondering in terms of quota, what uses less; a full replacement on the collection item, or your suggested answer.

Cheers :blush:

Since the latter example uses Update twice, it will require 2 Write Ops.

If you modify the Document once using Replace it will only cost 1 Write Op, but then you have to provide the entire data object. Is this what you mean by “a full replacement on the collection item”?

The queries shown do not cost Read Ops. The Update and Replace functions return the Document they modify, but at no additional Read cost.

Yup, that’s what I meant.

Thanks for clearing things up. The help has been much appreciated.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.