Delete a single field from an array of objects in Firebase

Hello everybody,
I have a problem that I can’t solve:

in my app I have a list of categories and then some product sheets. Each product sheet can have multiple categories. This is how this is stored on the DB:

When I delete a category from its list, I want to be able to delete the reference to that category from all the tabs that contain it.


To do this I thought of calling a record collection and filtering all the cards that contain that category, but being stored in the form of an array, the filter doesn’t work.

Any of you have any idea how to do it ?!

thanks a lot

update:
adding a data variable to the stream allowed me to filter the field I needed.

Now the problem is: how do I delete that field from the array ???
Maybe with an update record? But how do I tell appgyver to just delete index “0” or index “1” and so on ???

is there anyone who can help me figure out how to delete an element in a firebase object array like in the screenshoot? thank you very much !

Use a formula like :

REMOVE_ITEM_BY_KEY(tagCategorie, "token", ISTITUTI)

where “ISTITUTI” is possibly a dynamic value like repeated.current.item

Hi @Fred_Kuzyk , in which part of the flow to use this formula ?? Thank you very much !

I have to take back what I said in the last update post. Adding a Data Variable didn’t help me. Fred’s (@Fred_Kuzyk) solution, on the other hand, seems logically the best but I don’t know how to apply it. From what I understand, what I should do is:

create a Get Record Collection (this will retrieve all documents in the collection) → apply the REMOVE_ITEM_BY_KEY (list, key, value) formula to delete all matching fields.
It’s correct ? If YES, where should I apply this formula?

First, not sure you need a Set Page variable, you can work direct on your data variable.
Second, the suggested formula goes inside the update record flow function.
image

Thanks @Fred_Kuzyk, I’ll show you the configuration I have and the next error:



thanks again for your help!!

You are facing the same challenge I am facing every day as a beginner, when juggling between objects and arrays…You’ll have to do some serious research and reading until you master this topic. I’m at the same phase now.
All I can say is Your data variable is an object that contains properties like string values and arrays (like tagCategorie), while the formula returns an array (list) since it modifies a list.
You either need a variable that fetches only the array inside of the data var object and apply the formula to that list, or find a compound formula that lets you lookup inside the object for the tagCategorie array so you can apply the REMOVE_KEY formula.

Try this cheat as a workaround.
Create a new schema in the Firestore data resource (same collection name as the main one) where you have only:

{
id,
tagCategorie
   [
   {token: "text"}
   ]

Then in your update record, find the tagCategorie list binding and input the REMOVE formula.

Hi, thanks for your advice, but it’s not clear to me how creating a new schema in Firestore Data Resource can help me.

What I can’t do at the moment is given a collection of data, select only those that meet a certain condition (for example “tagCategorie contains ISTITUTI in the Token entry”. From here I could start and then try to delete the item from the document.
Another problem is that I have multiple documents that can contain that entry in Category tags and I have no idea how to iterate over each one.

Add new data resource with this type of schema, using the same collection name.
Create a list of object named tagCategorie (instead of ‘services’ in my image) with token as the single object.


Let’s address this challenge first.

ok done


Right, now you create the data variable and attach the update record flow function like before and assign the REMOVE_ITEM_BY_KEY(tagCategorie, “token”, ISTITUTI) to it, where istituti is a dynamic binding.

ok i followed your suggestions.
I created a data variable called “schedaDellaScienzaOnTagCategorieDataVar” linked to the data source you suggested.

I then created a record collection that points to the new data source. Next I set the data variable on this record collection.

At this point I add the Update Record function.

But there are some points that I can’t fill, besides the Remove_item_By_Key formula still fails.

Where am I wrong ??

p.s.:
There is one other thing that does not add up to me:
if I try to read the data recovered from the new data source you suggested me to create, I get the entire content of the documents in the collection, but if I guessed your logic, I should only get the object tagCategories with its list of objects .

Claudio,
You need to do some serious reading of the documentation. It looks like you are trying to build something on the fly, with zero reading/knowledge, this won’t work with AppGyver.
You do not need to add the get record collection and data var again to the button canvas, since they are already loaded on the pageFocused event.
So your update record function should be directly attached to the onTap event.

The formula I suggested was an example. The list should be a dynamic binding - from your data variable.

REMOVE_ITEM_BY_KEY(dynamic_list, “token”, dynamic_item)

@Fred_Kuzyk , I try to read the documentation at each step that I find difficult but for this type of situation I don’t seem to have read anything that can help me. So far the best help I’m finding is you!
I inserted a record collection after the tap event because I have to go to act on a database other than the one used in the page to show the data.
I’ll show you the complete picture so we understand each other:

when the user taps the trash, the category is deleted (“STITUTI” for example). This happens with delete records in the “Categoris” collection. At the same time, the system must read each document in the “SchedeDellaScienza” collection, find the ones where the “ISTITUTI” item exists (I will be dynamic) in the “tagCategorie” field and delete that single item.
This is why I add a get record collection in that location.

Actually all that block would go after the delete record, but now I have connected it to the TAP just for debugging convenience and only until I can get it to work.

Maybe I’m wrong …

To work across a list of documents, you need to use the FLATTEN formula.

hi, I had found the FLATTEN formula and it’s one of the things I’m experimenting with just right now.
Look at this though (I’m trying to simplify it as much as possible): I’ve created a data variable associated with my resource data for simplicity. In the get record I am trying to filter the data as I would like it (later I will move on to deleting the data that I do not need, I hope)



and I only get the document I need, but I have to STATICALLY indicate the two values in the exact position they are in.

This obviously is not good but if I could tell appgyver to check if TagCategorie contains 1 token element equal to “…”

obviously now if I launch this last example in the app (there are only 1 element in the array) then it does not work because he wants the list of all the elements to search and in the exact position in which they are stored (as you can see from one of the previous screenshots )

then I would be a step forward
I do not know if I was clear

Not sure what this filtering has to do with your goal to delete a field from an array. But I’m not familiar with filters, never used them.

I’d suggest the following formula for your purpose:
REMOVE_ITEM_BY_KEY(FLATTEN(data.data_resource_name_list), “token”, your_dynamic_item))