How to update value of element in an array stored as a FieldValue in Firestore - firebase

I stored an Array as a FieldValue in one of my document in Firestore. Now I want to update one of its element value. As per the documentation I can use arrayRemove or arrayUnion function to remove the element from array but I didn't see any method to update the value of element. Is there any way that I can help me to update the elements' value.
Here I stored array in "patients" field of document. This array represents list of patients. Look at 0th position of element. Here status's value is "current". I want to update this to "processed". Is there anyway that I can do so.
Also is there any way that I can query the elements of array on the basis of the value placed on status key.

As per the documentation I can use arrayRemove or arrayUnion function to remove the element from an array.
There is no way you can use one of those functions to add or remove elements from an array that contains objects. To be able to use those functions, your array should contain for example literal strings. In that way, you can add or remove one of the elements using arrayRemove() or arrayUnion() functions.
I didn't see any method to update the value of an element.
There is no update method. arrayUnion() method will add a new element in the array only if it does not exist. If you want to update an element, you have to remove it first from the array and then add the new one. This is also available in the case of strings and not in the case of objects.
Is there any way that I can help me to update the elements' value.
Yes, two ways in which you can achieve this. The first one would be to get the entire document, get the patients property as a list of hashmaps, iterate through its elements, make the desired changes and write the document back. The second one would be to transform that array into a subcollection where each patient will become a document. In this way, you can simply update add or remove a document using the corresponding functions.
Also is there any way that I can query the elements of the array on the basis of the value placed on the status key.
Using your current document structure, no. If you want to query the patients of a user based on the value of a specific property, you should definitely use the second approach. There is no way you can achieve this using your actual schema.

Related

kotlin firebase remove child

I'm trying to delete a specific value from a firebase realtime database, but I don't know how to do it because I don't know to save or find the key value of the child which is automatically generate.
If you see the picture I've only managed to remove all the children from the first key with
FirebaseDatabase.getInstance().reference.child("Comentarios").removeValue()
But I need to delete just by the child creadoPor
Is there any way of skkiping an unnamed child?
FirebaseDatabase.getInstance().reference.child("Comentarios").removeValue()
But I need to delete just by the child creadoPor.
Since you know the "grantparent" key of the data and the value of one of the nodes properties, you can use a query to find the nodes that match that value.
FirebaseDatabase.instance
.ref("Comentarios")
.child("-NGi7xP...")
.orderByChild("creadoPor")
.equalTo("R7lji3...")
When you get the DataSnapshot from the query, you'll need to loop over its children as shown in the documentation on listening for value events. Even when there's only one result, you'll get a list of one child node and thus will need to loop over them.

FieldValue.arrayUnion: How to add the same elements to an array? Flutter

I have created a review system, in a collection, within a document. In the document I have an array where the average of the ratings is (which when uploading the review are added), but if I upload two equal numbers with FieldValue.arrayUnion, I only get one.
That's the expected behavior of FieldValue.arrayUnion(). According to the API documentation:
Each specified element that doesn't already exist in the array will be added to the end. If the field being modified is not already an array it will be overwritten with an array containing exactly the specified elements.
FieldValue.arrayUnion() will not add an element if it already exists in the array. If you need to do that, you should read the document, modify the array in memory to contain what you want, then update the entire array back to the document.

Firestore Security Rule limit field entry

In my Firestore document, I am trying to limit the fields that the users could update to only the ones I allow. I thought about a function that could check if request.resource.keys() contains any keys that are not the ones I allow, if it does, I block update, if not, I allow update. so far, my function looks like this:
function field_limit() {
let allow_keys = ["field1", "field2", "field3"];
return request.resource.keys() in allow_keys;
}
inside the path match:
allow update: if field_limit();
When I run a unit test in Emulator that modifies "field1", it does not pass. I think I am doing something wrong here because I am comparing the whole keys list against the allow_keys list.
What should I do in order to achieve the desired functionality then?
I thought about a solution to iterate through request.resource.keys(), and see if each key is in allow_keys. If one isn't, block update. But how to traverse through each value of a list?
edit. Should be request.resource.data.keys() instead of request.resource.keys(). Otherwise, it won't work.
The in operator doesn't work the way you're expecting. See the documentation for that - it only checks if a single value is in a list.
If you want to check if a list of values contains only a subset of values in another list, you should use hasOnly instead.
request.resource.keys().hasOnly(allow_keys)
This will evaluate true if the list request.resource.keys() contains only values from the allow_keys list.

remove array item from nested map object in firebase firestore

I am trying to figure out how i would go about removing and adding to the profile array in the array of objects. I have the id's to get to the appropriate items in the arrays but it looks like i can only update the entire root array item.
doc().update({
technologyLearned: firebase.firestore.FieldValue.arrayRemove("?")
});
if this was a normal language i could technologyLearned[id].profiles[profileid] and get to the item i need to remove or add.
Since you can't index into an array to locate an item to remove, you will have to read the entire document, modify the array in memory, then write it back to the document.
The problem here is that technologiesLearned is an array (not that profiles is an array). If profiles was not nested under technologiesLearned array, you could use arrayRemove on it.

Is that possible to update Firestore document child array item only

I need to increment or decrement filed maxQty and the data structure is added in the below images.
Image with the red mark is the filed and I added another image to understand the data structure
Is there any way to do that?
According to the official documentation regarding updating array elements:
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements.
Unfortunately, arrayUnion() does not apply to your use-case, as your businessCard array contains objects and not strings. There are two options, one would be to read the entire array, increase the maxQty, and then write the document back on the server. The second one would be to update the document with the help of a Map by manually copying values into it for each of the fields you want to change.
Please also note that the update operation is not compatible with the automatic field mapping that occurs with Java POJO objects. You are allowed only to use Map objects.

Resources