Delete a single field from an array of objects in Firebase

I actually edit or remove this auto loading thats created when you add a new data variable, and control the loading in the rest of my flows. I find it easier to keep track of. I can more easily loop until everything has been loaded correctly.

Not relevant to the question being answered, just a passing thought.

1 Like

Hi I followed your suggestion.

FLATTEN does not seem to work for this problem.
I tried to pass my data variable to FLATTEN to display the result on the screen but this is the alert I get:

I also tried to enter the entire formula you suggested in the RECORD field of Update Record and this is what I get:

Either way, there is another obstacle here. Update Record requires the ID of the document to be edited, but I don’t have any ID to give it as I can’t create a filter that returns me only the documents that contain the element I need to delete (in this screenshot it is indicated as static but obviously not good).

Maybe the schema of your documents are not the same. They should be for FLATTEN and REMOVE to work.

I can’t help much as I don’t know the entire structure of your firebase neither the logic flows of your app.

My technical knowledge is still limited (I come from another PHP and MySQL based no code app) so I hope someone else can help.

thanks for your help so far. It seems no one else has experience with anything like this. :frowning:

however the thing I really can’t understand (regardless of how the formula works) is how to tell UPDATE RECORD that it needs to update more than one record.
In fact, Update Record requires the ID of the document to be updated as a parameter. A single ID.

The problem is that I will have so many documents to update. How do I pass them all to him? It would take a forEach loop or something similar but I don’t find anything about it in the appgyver documentation nor on the forums.

So even passing the formula REMOVE_ITEM_BY_KEY (FLATTEN (data.data_resource_name_list), “token”, your_dynamic_item)) in the Record property however the component will not work because it is missing ID !!

UPDATE is for updating a document so of course it requires an ID.
The pb is how you’ve structured your database. Having a user “delete” the same item (ISTITUTI) across many documents with the same schema makes no sense.
That’s why I invite you to read more about data modeling (firebase documentation) and AppGyver documentation.

Hi Fred,
it makes sense for my user to delete ISTITUTI because “ISTITUTI” is a category. Multiple documents can contain that category, so if the user decides to delete that category, all documents that contain it should be updated accordingly. Why do you say it doesn’t make sense?

I understand that.
It is the structure of the firebase data that seems not right. I don’t think you should have the same category pertaining to the same user across many documents.

Hi Fred, thank you again for your time that you are dedicating to me. I’ll explain briefly and just for clarity. It is not the user who has the category but the card. The user creates the card and assigns one or more categories to it. Nothing simpler. Therefore it follows that n cards correspond to 1 category (1-n relation). Normally with any other programming language and a relational DB like SQL this would be manageable with a few iterations on the data, but unfortunately not in this case. It is a pity.

AppGyver works well with relational DB. You could use baserow.io easy setup.
What you do is manageable with non rel DB but you need expert help. This forum is sadly not the place for that. Too few “angels” willing / having time to help.

I will take a look at baserow, perhaps for a future project (on this I am too far forward to radically change the database). I will find an alternative way to handle the problem (less performing for the UX but functional). It is really a pity that there are very few experts here. In any case, I thank you again for the time you have dedicated to me.

For help about firebase data modeling, ask the Firebase community at stackoverflow.
They were helpful with me when I needed confirmation for my own data modeling.
I strongly believe your current data structure is the cause of your problem.

This can be done quite simply. We have a football pool app where we do the exact same thing. We have a document for example: Season.2022

As information changes with the game i.e. scores, spread, game status etc. we update the entire array on it own inside a page variable and when we update the record, we update the entire array each time and re-write the entire document. Should be very simple to pull the array from each record, search for your field value, do something with it (change value, delete index, whatever) then update the record by adding the entire array into the map field along with updating the other fields in the document. With firebase, not sure you can change a value inside an array that’s inside a document. I think you must re-write the entire array.

This should help:

Let us know if this helps you.

Hi Dave, thanks for your input. I read Alessio’s article (the one you linked to me) and it is very interesting, but I see that there is a lot of code, which would be fine if I were writing my app in Flutter or React Native I suppose ?! But how do I replicate it in AppGyver? I am very interested to understand how you solved the problem with your football pool app on AppGyver, if you feel like sharing it here with us. Thank you!

What we did in our app and what you need to do in your app is similar but not exactly the same. If you are looking for exactly how you can do this in your app, no one will be able to answer without having access to your data and your app project. But the concepts are the same.

A lot of this depends on the scale of your app. In our case we have about 50 users, each with their own document so it’s not hard to load all of their data every time the app loads. If we had 500 or 5000 users we would be approaching this problem a very different way.

How many documents will these categories be stored?

If you want to remove the category from every document that means you must cycle through every document, check to see if the category is in the array (map field), remove the category, then update the document with the new array.

Step one ‘Set the data variable’ YourDataVariable to a single document, for example:
SELECT(ListofDocs.ID == appVars.SomeVariable.ID)
replace the variables your own. the idea is to get a single document to check.
Then ‘Set Page Variable’ YourArrayVariable
data.YourDataVariable[0].tagCategorie
now your data variable contains the array.
Once the variable contains the array, do as @Fred_Kuzyk suggests
REMOVE_ITEM_BY_KEY(YourArrayVarialbe, “token”, ISTITUTI)
Then update the document you are working with,
using Update record for your data variable.

All of this needs to be in a loop. you create with an IF statement for example:
This is how you would create a loop, this one is triggered by a button press, but you get the idea.
IF(pageVars.varIndex < COUNT(pageVars.varNumberofDocuments),true,false)
if true:
Step one ‘Set the data variable’ YourDataVariable to a single document, for example:
SELECT(ListofDocs.ID == appVars.SomeVariable.ID)
replace the variables your own. the idea is to get a single document to check.
Then ‘Set Page Variable’ YourArrayVariable
data.YourDataVariable[0].tagCategorie
now your data variable contains the array.
Once the variable contains the array, do as @Fred_Kuzyk suggests
REMOVE_ITEM_BY_KEY(YourArrayVarialbe, “token”, ISTITUTI)
Then update the document you are working with,
using Update record for your data variable.

if false:
Do Something else
Here’s an example of a loop:

But again, this will all depend on scale. Works for us because we are small and are going to stay small. Could get costly at scale depending on the number of document reads you are generating.

1 Like

Hi Dave,
of course i don’t expect anyone here to tell me exactly how to do on my app but all the tips\practical examples can help.
From what I understand, you create for each document a supporting variable exactly identical to the document, then overwrite that with the new update data (single row or field) and then overwrite\update the DB record with that same variable. This is because firebase does not allow the updating of the individual fields\row of a record but only of the entire record (as I read in Alessio’s article). Did I get it right? It’s correct?

You actually can update / overwrite single properties on Firebase, with the REMOVE formula I shared earlier.

It is impossible to tell you how to do it exactly since we do not have access to your entire system (schema, inputs, flows…).

Does it count as ‘one read’ as per Firebase or as ‘as many reads as there are properties being updated’?

Yes, in a nut shell that is correct, you should create a variable to hold the array portion of the document. Update the array variable, add, subtract, change etc. get the array variable like you want it and then when you update the document the map field binding is the array variable. And Boom, Bob’s your uncle!

Are you sure?

The documentation and stuff I’ve read says no.