0

I create city documents where I save some fields like title and description. Is it possible to add some fields later to a specific city b y knowing the city title for that db.collection path instead of the id?

First elements added

public createSavedCity(title: string, desc: string): void {

const cityFields = {
  title: title,
  description: desc
}



this.db.collection('saved').add(savedFields);}

I will use a new function to update the document which will receive as a parameter the title of the document I want to update

updateDoc(savedTitle: string, newItem: string) : void {

  this.db.collection.(`saved/${savedTitle}`).update({newField: newItem});

}

1 Answers1

0

Yes, you can query for documents using that title, iterate the results, and use the update() method to change the contents of an existing document using DocumentReference.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I can pass the title of a city as a parameter to the function that will update the current document. In this function how should the path look? Will this path be correct? this.db.collection.(`saved/${savedTitle}`).update(); –  Nov 06 '19 at 15:37
  • That will not work. The only way you can build a document reference is if you know the ID of the document. You can't use a value of a property in that document. You will first have to query for the document in order to find its reference, then update it. – Doug Stevenson Nov 06 '19 at 15:55
  • So I need to query the collection to find the city with that title, and then pass that city id to my function in order to update this city fields –  Nov 06 '19 at 15:57
  • Yes, or you can use the document reference available from the query snapshot instead of building a new one. – Doug Stevenson Nov 06 '19 at 15:58
  • Thanks for the guidance –  Nov 06 '19 at 16:07