Firestore DB: Create collection inside a document using REST API

Firestore DB: Create collection inside a document using REST API

Problem Description:

I’m creating an app that requires me to create a new collection inside existing document. I know it can be done on the firestore DB with a few clicks.

I want to do the same with a REST API

Something like this

Is there any way to implement this using a REST API

Thanks!

I tried looking for docs in the official website but couldn’t find anything.

Solution – 1

To create a subcollection using the firestore REST API, use the url below.

NB// Replace all variables that start with YOUR

https://firestore.googleapis.com/v1/projects/<YOUR_PROJECT_ID>/databases/(default)/documents/<YOUR_COLLECTION_NAME>/<YOUR_DOCUMENT_ID>/<YOUR_SUBCOLLECTION_NAME>?documentId=<YOUR_RANDOM_ID>

Also, use the json example below for the request body because this is a POST request.

{
  "fields":{
    "name":{
      "stringValue":"This is a new document in a subcollection"
    }
  }
}
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject