0

I have a document which contains an array of object. The document is meant to store relations between two individuals on the app. They key defining the relation is status. example one document is:

{
   "document":{
      "relation":[
         {
            "displayURL":"https://firebasestorage.googleapis.com/something.png",
            "status":3,
            "to":"fDI5KVG8xsMz3wbviiiumEeZzYT2",
            "onClick":"fDI5KVG8xsMz3wbviUNumEeZzYT2",
            "from":"Aman Sharma "
         }
      ]
   }
}
Now suppose I want to change the status from 3 to 2. The only thing that comes to my mind is using FieldValue.arrayUnion which is as follows.

public void setRelation(String from,String to,String displayURL,int status) {

        FriendRequest friendRequest=new FriendRequest(from,to,displayURL,status,currUid);
        Map<String, Object> update = new HashMap<>();
        update.put("relation", FieldValue.arrayUnion(friendRequest));

        friendReqs.document(currUid).set(update, SetOptions.merge()).addOnCompleteListener(completionLiveData);
    }

This essentially creates a new object in relation array, with a status equal to 2. But I want to just update particular object in relation array without creating a new one.

1 Answer 1

1

You will need to read the entire Array in your front-end, modify it and write it back.

Note that, on the other hand, it would be possible if you use a Map instead of an Array, see Update fields in nested objects.

Sign up to request clarification or add additional context in comments.

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.