Search code examples
gofaunadb

How to delete a document found by a search in the FaunaDB?


I can get a searched document as following golang code:

ret, err := client.Query(f.Get(f.MatchTerm(f.Index("label_search_by_externalID"), externalID)))

Then, I tried to delete a searched document as similar manner as follow:

ret, err := client.Query(f.Delete(f.MatchTerm(f.Index("label_search_by_externalID"), externalID)))

But, this code occurs an error:

Response error 400. Errors: [delete](invalid argument): Ref expected, Set provided.

I'm confused, by the API document, both Get and Delete request Ref for a document as param, and MatchTerm returns a Set, not Ref. Then I have 2 questions.

  1. How can I get the document Ref which the result of the search? Is there any way to get the document ref from the search result with Index like RefCollection for collection, for example like RefIndex as follows?
ret, err := client.Query(f.Delete(f.RefIndex(f.Index("label_search_by_externalID"), externalID)))
  1. Why my code for getting search result document works well? Is there more good coding for getting search result document?

Thank you for your suggestion!


Solution

  • Get will only return 1 result. If you have multiple values returned from this index search you will have problems. I would suggest the function Paginate(). This will return a set of results, which you can then map over and execute other functionality, such as a get() or delete(). I would strong suggest you look at the following tutorial (https://docs.fauna.com/fauna/current/tutorials/indexes/pagination).

    This is pseudo code to delete a page of documents found. You can adjust the page size either up or down:

    Map(
      Paginate(Match(Index("label_search_by_externalID"), true)),
      Lambda("X", Delete(Var("X")))
    )