2

I have a collection of users. Each user has an array named favoriteNames.

root <- [Firestore]
 users <- [Collection]
   uid <- [Document]
    favoriteNames: ["Ana", "Jane", "Dave"] <- [Array]
   uid<- [Document]
    favoriteNames: ["Ana", "Merry", "John"] <- [Array]

I want to remove "Ana" from all documents. This is what I tried:

usersRef.whereArrayContains("favoriteNames", FieldValue.arrayRemove("Ana").get()

But it doesn't work. How can I remove "Ana" from all arrays?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Lis Dya
  • 317
  • 3
  • 14

2 Answers2

3

It's not possible to do a SQL-like "update where" type query with Firestore. If you have multiple documents to update, you will have to update each one individually. You can use a transaction or batch write to do all the updates atomically, if needed.

Minimally, what you will have to do here is:

  1. Query for all the documents to update. This will involve doing an array-contains query for all "Ana".

    usersRef.whereArrayContains("favoriteNames", "Ana")
    
  2. Iterate the query results.

  3. For each matching DocumentSnapshot, get its DocumentReference and use update() with FieldValue.arrayRemove("Ana")

    snapshot.reference.update("favoriteNames", FieldValue.arrayRemove("Ana"))
    
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you Doug for reopening the question. I think the one who closed it didn't really understand what I was actually asking :( I see now, there are two steps required to perform the update in all documents. This is what I was looking for. Saved my day!!!!! – Lis Dya Jul 01 '20 at 15:21
1

If I'm not getting your question wrong you want from one array remove a specific item, in this case "Ana". I've created a sample doing so, let me know if is what you are looking for.

fun main() {
  val a = arrayListOf("Joan", "Skizo", "Ana")
  val b = a.minus("Ana")
  print(b)
}

What is printing is

[Joan, Skizo]

To do so, you can use minus

Returns a list containing all elements of the original collection without the first occurrence of the given element.

Regarding this answer you'd have to read, write what you want and then remove them from the list and then write it back.

Also this might help you with the update method I'd say : https://googleapis.dev/nodejs/firestore/latest/FieldValue.html#arrayRemove-examples

This question might help you also remove array elements in firebase

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Thanks but those arrays are actually in Firestore. Please check the edit. – Lis Dya Jul 01 '20 at 12:11
  • According to the first answer example, I am passing the entire object to `arrayRemove` function. There's nothing missing in this object ("Ana"). The second answer is only updating a single document, I need to update all documents in a collection. `whereArrayContains` is a Query, returns multiple documents. – Lis Dya Jul 01 '20 at 13:12
  • @LisDya you need to perform this operation programmatically, as is mentioned in this answer https://stackoverflow.com/a/51484348/11529321 – Jan Hernandez Jul 01 '20 at 14:07
  • Exactly you need to add an `update` block and then remove the "Ana" thingy – Skizo-ozᴉʞS ツ Jul 01 '20 at 14:25