0

I am using Firestore by Firebase. There I am using the ArrayList. I save a lot of numbers in a few ArrayLists. But now I have the problem, that I can not save a number twice in an ArrayList.
Then I found this where it is described(docs): https://firebase.google.com/docs/firestore/manage-data/add-data
"arrayUnion() adds elements to an array but only elements not already present"

Is there really no way to save a number twice or more times in an ArrayList in Firestore?

But an interesting thing is that I can add the values at my dashboard, but the user through code not.

EDIT:
I am adding the numbers like this to the ArrayList:

gradesRef.onSnapshot((doc) => {
          gradesRef.update({
            [grade_type]: firebase.firestore.FieldValue.arrayUnion(grade)
          });
        });

2 Answers 2

2

In JSON and in most programming languages an array can contain the same value multiple times.

But many developers use arrays to implement mathematical sets, which are a collection of unique values. That means that in most programming languages you end up with this operation in many places. In JavaScript for example:

if (!array.contains("my new value")) {
  array.push("my new value");
}

You now have two statements to accomplish one operation, which leads to a race condition (especially in multi-user scenarios).

For this reason Cloud Firestore added a atomic operation array-union, which adds an item to an array if it isn't in there already. You should use that operation if you want to have unique values in your array.

If you don't want the values in your array to be unique, you should use the normal array operators of your programming language. In JavaScript this would simply be the push() operation we used above, but without the if around it:

array.push("my new value");

Update based on your code sample.

gradesRef.get().then((doc) => {
    let grades = doc.data()[grade_type] || [];
    grades.push(grade);
    gradesRef.update({
        [grade_type]: grades
    });
});

So:

  1. Get the existing grades array from the document
  2. Add the new item to the end of the array
  3. Write the updated grades back to the database.
Sign up to request clarification or add additional context in comments.

4 Comments

Can you please look at my edit and say me how to rewrite it that it works?
You're still using arrayUnion. I updated my answer with a sample.
When With this code I get the following problem: My ArrayList has only one number. Number 8. Then I wanted to add again number 8 with your code. I expected that in the ArrayList there should be two times the 8. BUT there where to many eights. See here: dropbox.com/s/awx6dg1fjl38u5a/tt.PNG?dl=0
Ah yes, that's because you're using onSnapshot. So every time you update the document, it triggers the same onSnapshot, which causes another update. You probably want to use get(). Note that such things are easiest to troubleshoot if you run the code in a debugger and check what's going on.
0

Why would you want to add the same data twice?

If it's really different data that happens to have the same value, consider creating an object that contains a unique id and the value, and add a new object containing the duplicate value with a new id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.