How to remove a key from object

I’ve read the docs, the cheatsheet, done plenty of searching. I know I can use Replace to update the data of a document, but I don’t want to query the database, do logic, then query again. If I can just send logic over to remove a single key from a single object it saves a lot of time. I have a document that looks like this:
{
“ref”: Ref(Collection(“users”), “272036256946323968”),
“ts”: 1595813497670000,
“data”: {
“email”: “…”,
“providers”: {
“twitch”: “173751571”,
“youtube”: “114204306776175654265”
}
}
}

Now, given the name of a key in “providers”, how do I remove the key from the object and make no other changes? I have indexes that already verify its existence, so that’s not a concern. I simply need to remove the key without affecting the rest of the document. I could not find a simple way to do this via FQL.

Maybe you could use Merge function to remove the key, something like that:

> Merge({key1: 1, key2: 2}, {key2: null})
{ key1: 1 }

Does that work for you?

> Get( Ref(Collection("foo"), "1") )
{
  ref: Ref(Collection("foo"), "1"),
  ts: 1595860710340000,
  data: { key1: 'foo', key2: 'bar' }
}

> Let(
 { x: Ref(Collection("foo"), "1") },
 Replace(
   Var('x'), 
   { data: Merge(Select('data', Get(Var('x'))), {key2: null}) }) 
)

{
  ref: Ref(Collection("foo"), "1"),
  ts: 1595860748070000,
  data: { key1: 'foo' }
}

> Get( Ref(Collection("foo"), "1") )
{
  ref: Ref(Collection("foo"), "1"),
  ts: 1595860748070000,
  data: { key1: 'foo' }
}

Yes, thank you! I didn’t consider that “null” could be used to remove a key. I figured it would just set the key to null.

1 Like