1

My data looks like this

"countries": [
  0: [name: "USA", capital: "Washington"],
  1: [name: "Canada", capital: "Ottawa"]
]

If I want to delete the countries where the capital is "Ottawa", how do I do it?

I tried

currentDocument.updateData([
  "countries": FieldValue.arrayRemove("Ottawa")
]) { err in
  if let err = err {
    print("Error updating document: \(err)")
  } else {
    print("Document successfully updated")
  }
}

But this just deletes the entire array. Even if I try to target the specific country ("countries.1.name": FieldValue.arrayRemove("Ottawa")) it doesn't work and just deletes the entire array again.

What am I doing wrong?

Deviyani Swami
  • 749
  • 8
  • 17
Richard Reis
  • 219
  • 2
  • 10
  • The problem is that I don't always know what index the country is in. So normally it would be something like `countries.1.capital` but I want instead to loop through the array and find the `country` with the `capital` `"Ottawa"` – Richard Reis May 18 '19 at 06:45
  • What is FieldValue? Show arrayRemove code – RajeshKumar R May 18 '19 at 10:34
  • @RajeshKumarR the way I can get the value in FireStore. I got it from the documentation https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue (I’m trying to make it select “Ottawa” as the value but it doesn’t work) – Richard Reis May 18 '19 at 10:36
  • There is no single operation that will do this. You will have to read the document, update the array in memory, then write the modified contents back to the document. `FieldValue.arrayRemove()` only works when you know the entire contents of the item in the array, which means you will need to know both `name` and `capital`. – Doug Stevenson May 18 '19 at 21:34

0 Answers0