How i write multiple field in Firebase?

How do I…

I’m trying to push all my player variables that are in a structure into Firebase

What is the expected result

Globally save my stats

What is the actual result

I’ve found two methods, WRITE A FIELD, and WRITE A DOCUMENT
Writing a field is working, but not efficient, i have to multiply the calls and it will be hundreds.

Writing a document seems quite nice on paper, but i can’t make it work, i don’t understand what is expected as “variable” ? so i got errors

Related screenshots

Thanks a lot for your help

1 Like

As far as I know, WRITE A DOCUMENT does not help to save more things at once and the action has never worked for me.

In most cases the best solution is to convert the main structure variable to json (expression) and save it in one field. (non-existent documents are created automatically)
When loading, use the json action back into the structure variable.

Hum… Thanks for that answer.
What do you think is best in term of performance ?

  1. One big Structure in JSON saved in one document and one field ?
  2. I Split my big structure in smaller structure parts like : users, inventory, equipment, … and each one is, as you said a JSON ?


I don’t know about performance, but I would make several.

A document is a structure variable, it is a collection of fields and values. This error means your “temp_user” is not a structure variable. Make sure you pass in a structure variable.

I would recommend against that, as it is bad for performance, bad for observability & control (you cannot easily visualize and edit your data in the firebase console), more expensive (as information is stored in an inefficient way leading to more storage consumption), it makes it impossible to use query filters with the database fields, and maybe most importantly, you cannot have security rules this way.

Understand your point but i cannot make it work even with a structure …

Also compare to the write field there is not the “merge” option, so if this work as it should what would be the behavior ?

Please show your events, I just tested to confirm and the action does function as intended on the firebase example project:

The “write” action will create or overwrite an existing document, replacing a previous document with the same name. To make changes to multiple fields of a document at once without touching the other fields, similarly to the behavior of the “merge” option, use the “update document” action.

I thought you often recommended JSON for this.
Should he really use a separate “Write a field” and “Get a field” action for each of the hundreds of values? Maybe I’m misunderstanding something, but that certainly wouldn’t be good.

It sounds like he only wants to load everything once when the game starts, not individual things while playing.

I wouldn’t generally recommend using the actions to read or write a single field, as they unnecessarily consume more reads and writes in the Firebase quota, and are less efficient.

My recommendation is to fit all the data you need in a single structure variable and to write the entire structure variable as a document in Firestore, not as a JSON string, and to read the entire document as well when you need to access it with “Get a document from Firestore”.

1 Like

Thanks actually… It took me a while but indeed it’s working and way more easy.
We can close this ticket.