0

enter image description here

I want to remove a specific field (it's user id) from the array in Firestore.

I try:

 override fun unlikePost(postId: String) {
        FirebaseFirestore.getInstance().collection(postCollection)
            .document(postId).update("likeList", FieldValue.arrayRemove(FirebaseAuth.getInstance().uid))
    }

But when I do this my likeLIst in Firestore change to:

[]

Why unlikePost doesn't remove specific field but clear whole array and change to []?

2
  • Were there any more values in the field, than the one you're showing in the screenshot? Commented Jun 7, 2021 at 22:17
  • Yes, there were. When I call my function it removes all fields from array list. Commented Jun 8, 2021 at 17:56

1 Answer 1

2

How to remove a field from an array in Firestore?

A call to:

.update("likeList", FieldValue.arrayRemove(FirebaseAuth.getInstance().uid))

Does indeed that. This means that you are removing the UID of the authenticated user from the "likeList" array. However, this doesn't mean that you are removing the array entirely from the document, hence that representation. The two brakes [] represent an empty array.

So how can you remove the entire array from the document? You might think that passing a null value, might do the trick, but it won't, since null is a supported data type in Firestore. So a call to:

.update("likeList", null)

It will lead to:

Firestore-root
   |
   --- posts (collection)
        |
        --- postId (document)
             |
             --- likeList: null (array)

If you want to get rid of the array entirely from the document, you should use a call to:

.update("likeList", FieldValue.delete())

In this way, your array will be deleted from the document no matter how many UIDs exist in the array.

Edit:

According to your last comment:

For example there is 5 UID in the array. I want to remove only a specific one. Now when I try to do that I clear the whole array.

Let's assume you have an array that contains two UIDs, one is "abc" and the second one is "xyz". The last one being the UID of the authenticated user. If you want to remove "abc", you should use a call to:

.update("likeList", FieldValue.arrayRemove("abc"))

If you want to remove the second one, you should use:

.update("likeList", FieldValue.arrayRemove("xyz"))

Or:

.update("likeList", FieldValue.arrayRemove(FirebaseAuth.getInstance().uid))

Because FirebaseAuth.getInstance().uid returns "xyz".

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

6 Comments

I think there is a misunderstanding. I don;t want to remove or clear array. I want to remove specific item from array list(in that case it's current user ID)
As long as you initially had an array that contains the UID (PNke...7vK2) and you have used .update("likeList", FieldValue.arrayRemove(FirebaseAuth.getInstance().uid)) to remove it, the array remained empty, right? Then what's the issue?
For example there is 5 UID in array. I want to remove only specific one. Now when I try do to that I clear whole array.
Please check my updated answer. Is it ok now?
@Wafi_ck If Alex's answer doesn't explain the solution yet, can you edit your question to show the before and after screenshots of a document to show the problem?
|

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.