Val
Val

Reputation: 325

Firestore and flutter: update documents when a field is null

I have a collection which represents a list of available sport matches (see image below, sorry for the italian text). Each document is a match, and has a list of players which are subscribed to that match (id_player1, id_player2, etc).

When someone would like to subscribe to that match, I have to cycle through the players_id, and when I find a null one, I set it to the user's id.

So my questions are:

enter image description here

Upvotes: 1

Views: 595

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599571

If there is no specific meaning to each individual id_player* field, consider storing all player IDs in a single player_ids array field.

That way you can use arrayUnion to add values to the field (preventing duplicates) and query with array_contains to find documents with a specific player ID.

Upvotes: 1

Patrick G.
Patrick G.

Reputation: 475

You decide to define 6 different fields to store players id.. so u cannot cycle that fields.. what you can do is to get all of the six fileds and check one by one if they are null...

what you should do is to refactor that logic and store players id in a collection.. an update the collection only if its count is under 6 so you haven't t check if you have any space left to add player id

Bye :D

Upvotes: 2

Related Questions