How to delete a record against a data field which is not Ref.id?

Hi there,

I have a table called subscriptions, each record has this structure:

{
“auth”: “sdfdfssdfdg2324tdxcx”,

}

I would like to delete a record against the auth property rather than the ID of a record.

How can I do it?

Thanks

Use an index to get the id, then delete with the id.

that’s what I thought, but I don’t know how to get a reference to the id, because when I inspect the object returned from FaunaDB, it looks like this:

   {
    data:[
      {
         ref: {
                  @ref: {
                              id:"xxxxxx"
                   }
         }
      }
    ]
    }

I don’t think I could do something like:
const response = await client.....();
then use it like:
response.data[0].ref.@ref.id

so I ended up using Ref as Id to delete it.

First, don’t be fooled by the raw data format of the fauna response. data[0].ref can be used directly again in a new query.

In any case,

You should do the whole thing in one query, rather than get the id in one and then send another to delete.

Let(
  {
    ref: Get(Match(Index(‘ThingByAuth’), your_auth_value))
  },
  Delete(Var('ref'))
)

This assumes that auth is unique. If the index results in more than one value, then you need to Paginate and Map over it.

1 Like

Yep, this is the approach I’d like to apply to my code.
Thanks for the code example, I will try this and update this post with my result.

One thing I found challenging is that it’s hard to find code examples or tutorials, although I did find the API reference, but since I am new to NoSql and the whole concept, so that it’s hard for me to infer the code from just looking at API reference.
So hopefully we can see more and more code examples and tutorials adding into the documentation. :smile:

hi @ptpaterson
here is my update:

Error: ref is not defined

I tried the code in the Shell: https://dashboard.fauna.com/webshell/@db/

this is the error I got.

Also looked into the current Let API reference and came up with this expression:

Let(
  {ref: Get(Match(Index("sub_search_by_auth"), "s1RzQoX3U2aVy5jifhXFOg21"))}
  ,
  Delete(Var('ref').ref)
)

then I got a different error:

Error: [
{
“position”: [
“in”
],
“code”: “invalid expression”,
“description”: “No form/function found, or invalid argument keys: { }.”
}
]

could you please provide a code example?

I forgot that Get(Match(...)) will deref the ref, then you have to Select it. I also forgot to put the variable in quotes.

So, for what you were trying to do, you cannot “dot” anything like Var('ref').ref. That is what Select is for. i.e. Select(['data', 'ref'], Var('some_document'))

But we can avoid that by returning the Ref directly back from the index, which I should have known better to do.

Let(
  {
    matchRef: Select([0], Paginate(Match(Index('sub_search_by_auth'), 's1RzQoX3U2aVy5jifhXFOg21')
  },
  Delete(Var('matchRef'))
)
1 Like

thanks, hopefully it works this time. I will update you soon :slight_smile:

It works~!

I just added the searched value inside Match().

Let(
{
matchRef: Select([0], Paginate(Match(Index(‘sub_search_by_auth’), “the value”)))
},
Delete(Var(‘matchRef’))
)

Thanks a lot :smile: :+1: