Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create document Id for firestore documents using adminSdk in Firebase cloud functions?

How to create a document push id for firestore document using Firebase Admin SDK in Firebase cloud function.

I know how to do it in Angular using AngularFire2

const bookingId = this.afs.createId();

I need to do this thing in my cloud function.

like image 836
Ajay Sivan Avatar asked Jul 24 '19 19:07

Ajay Sivan


People also ask

How do I get the document ID added to firestore?

Set the data of a document within a collection, explicitly specifying a document identifier. Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier. Create an empty document with an automatically generated identifier, and assign data to it later.

Can documents have the same ID Firebase?

You cannot have several documents with the same ID in one Collection: a document ID must be unique across a collection.

Is Firebase document ID unique?

Correct, it is extremely unlikely, but not guaranteed.


1 Answers

Just call doc() with no arguments on a CollectionReference where you would like the document to live. You can then call set() on that returned DocumentReference to create the document.

If you don't even need to create the document and just want the random ID, use the id property on the DocumentReference.

(Realtime Database called these things "push IDs" because you used the push() method, but they're just called auto generated IDs in Firestore).

like image 104
Doug Stevenson Avatar answered Sep 20 '22 16:09

Doug Stevenson