Home > Back-end >  How to update a document in Firestore just using his unique id?
How to update a document in Firestore just using his unique id?

Time:01-08

Is there any way to update a document with unique id in Firestore that is in a collection, but without specifying the parent collection?

As you can see in the code below, I can specify the 'users' collection, uid and id. But I have no way to specify the parent collection of the element I want to update in the collection.

 await updateDoc(doc(collection(db, 'users', uid, unspecifiedCollection), id), {
        content
      })

Does anyone have an idea to solve this problem?

CodePudding user response:

To update a document, you have to specify the full path to that document. There is no way to update a document without specifying its full path.

In general a best practice when modeling data in Firestore is to use fixed/known collection names and dynamic/generated document IDs.

If you don't know the name of the collection, that typically means you should change your data model so that you always know them.

  • Related