add string to an array in Firestore - flutter - flutter

i have created an array of strings "challengeMember" in Firestore database and i want to add data to it when user clicks a button, my database:
in my code am trying to update the array, every time user join a challenge, the challenge name will be added to this array i know how to access the array using:
FirebaseFirestore.instance.collection("Users").doc(user!.uid).update({'challengeMember': widget._challengeName});
but the problem with this it is not specifying the array index and it is not checking whether the index is empty or not, so how can i dynamically access this array and keep adding values.

Firestore has a special trick to add an array, and it goes like this:
await FirebaseFirestore.instance.collection('users').doc(uid).update({
'tokens': FieldValue.arrayUnion([token]),
});
See the FieldValue documentation on how to manipulate arrays.

Related

Does MongoDB have set for data structure?

I have a list of user ids stored in a field of MongoDB collection. Each of the user ids is an UUID. The field is an array. For example,
["59cc7562-751b-11ed-a1eb-0242ac120002", "66e6f240-751b-11ed-a1eb-0242ac120002",
"742df8ea-751b-11ed-a1eb-0242ac120002", ............,"6cf6e398-751b-11ed-a1eb-0242ac120002"]
Now, if I want to check whether a user id is in the array, i need to do loop through the array until I find the user id or reach the end of the array. This is slow. I wonder if I can use a set to store the user ids instead of an array.
I am using Next.Js in the backend. If I convert the array to a set, that would be even slower than just looping through the array since the conversion takes o(n).

Query a Map containing a string saved in Cloud Firestore with Dart/Flutter

I've a problem with a query condition. I would like to query a string inside an array that's also inside a map. I manage to receive the information complet without the where condition but when I do this condition I've an error.
My function to retrieve all information
My array with a map inside
If category is the only value inside the produits array items, you can check for its existence with array-contains:
collectionRef.where('produits', arrayContains({ 'categorie': 'Alimentation' }))
If there are more properties for the array item, you will need to specify all of them in the call to arrayContains. There is no way to query for a subset of the array item.
In such cases, a common workaround is to add a extra array field to the document with just the property values you want to query for. For example:
produitsCategories: ['Alimentation', '...']
With that field in place you can then query it with:
collectionRef.where('produitsCategories', arrayContains('Alimentation'))

updating Map values in array flutter Firebase

here is the map and the vlaue im trying to update
the problem is when i try to update the imageUrl , the whole Map replacing with only ImageUrl new value
like this
what i want is to update imageUrl only , not effecting other data
this is what i do but not work
FirebaseFirestore.instance.collection('oorders').doc('MyDocId')
.update({ 'items.0.imageUrl' : 'new image Url' });
From the provided Firestore Database structure it is clear that you are trying to update an element inside an array field in a Firestore document. But currently Firestore doesn't have the ability to update an existing element in an indexed array. It supports only two methods, arrayUnion() to add an element and arrayRemove() to remove an element from the array as mentioned in the documentation.
So if you try to do something like the following -
FirebaseFirestore.instance
.collection('oorders')
.doc('MyDocId')
.update({'items.0.imageUrl': 'new image Url'});
it will delete the array element items and create another map named items with the field mentioned in the update statement.
As an alternative, you can read the entire array out of the document, make modifications to it in memory, then update the modified array field entirely. This is an error prone and tedious process. You can have a look at this article to know more about it.
I am not sure of your use case, but it seems you can make your database structure more suitable by using maps(nested objects) instead of arrays. Something like this -
By doing this you can update the nested objects by dot notation as mentioned here which will update only the mentioned field without deleting the other fields. The sample to update the document will look like this -
var item = 'item1';
var propertyToBeUpdated = 'imageUrl';
var newImageUrl = 'New image Url';
FirebaseFirestore.instance
.collection('collectionId')
.doc('documentId')
.update({'items.$item.$propertyToBeUpdated': newImageUrl});

How to increment individual element in an array in firestore for flutter

This is how my data look like.
I'm able to use FieldValue.increment(1) to update individual fields but is there a way to use FieldValue.increment(1) for specific individual elements in the array? Thanks in advance!
I tried using the following code:
firestore.collection('test').doc('I1raMaJArb1sWXWqQErE')
.update({
'rating.0': FieldValue.increment(1),
});
But the whole rating became empty as seen
You can't use FieldValue.increment() if the field is part of the array. The value of the field inside the array is fixed. The best way to update or edit the field that part of an array is to read the entire document, modify the data in memory and update the field back into the document.

How Can I Create a rule in firestore security rules for each item in my array

I can access a single item with its index like this
In this example i can access a single item in that array but i want the rule to be not for that single item but for each item in that array.
After looking for a solution for some time couldn't find anything and instead of storing the Posts in an array i just created a collection inside the Person document and stored them in there.