25

Is there a way that I can get the auto-generated ID for a document created as part of a batch using Firestore?

When using .add() I can easily get an ID:

db.collection('posts')
  .add({title: 'Hello World'})
  .then(function(docRef) {
    console.log('The auto-generated ID is', docRef.id)
    postKey = docRef.id
});

Using .batch() I can add a document with .doc() and .set():

const batch = db.batch();

const postsRef = db.collection('posts').doc(postKey);
batch.set(postsRef, {title: 'Hello Again, World'});

const votesRef = db.collection('posts').doc(postKey)
                   .collection('votes').doc();
batch.set(votesRef, {upvote: true})

batch.commit().then(function() {
});

Can I get the auto-generated ID of the document that was added to votes?

Update:

Doug Stevenson is correct - I can access the ID at votesRef.id

uturnr
  • 2,516
  • 2
  • 12
  • 11
  • Possible duplicate of [Firestore - batch.add is not a function](https://stackoverflow.com/questions/46725357/firestore-batch-add-is-not-a-function) – Julien Bérubé Jun 18 '18 at 14:54
  • Yes. You must instantiate your `ref` on each action. In other words, if you are iterating over an object or array, you must call `doc()` on each iteration. – Ronnie Royston Jul 24 '18 at 23:23

3 Answers3

37

When you call doc() without any arguments, it will immediately return a DocumentReference that has a unique id, without writing anything to the database - the id is generated on the client. So if you want that id, simply use the id property on that DocumentReference. That id will become visible in the database after you've written that document.

benomatis
  • 5,536
  • 7
  • 36
  • 59
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 6
    I'm following the Firestore web documentation example: `var newCityRef = this.afs.collection("cities").doc();` gives me an error "Expected 1 arguments, but got 0". This is completely stopping me from moving forward at all. – FiringBlanks Oct 29 '17 at 14:56
  • @FiringBlanks Maybe angular does something different than the standard javascript api? – Doug Stevenson Oct 29 '17 at 15:17
  • @DougStevenson It looks like they made it a required parameter. See here for more info: https://github.com/firebase/firebase-js-sdk/blob/d43d461aaa2496d4cd12fd593373f6dad214716f/packages/firestore/src/api/database.ts#L397-L408 – Edric Nov 05 '17 at 09:29
  • 1
    @Edric You're looking at doc() on the Firestore class. I'm referring to doc() on CollectionReference. https://github.com/firebase/firebase-js-sdk/blob/d43d461aaa2496d4cd12fd593373f6dad214716f/packages/firestore/src/api/database.ts#L1660 – Doug Stevenson Nov 06 '17 at 19:53
  • Maybe I am late on this, but now (2022) you can get the reference by using `ref = doc(collection(Firestore, 'collectionName'))`. The third example of this [link](https://cloud.google.com/firestore/docs/manage-data/add-data#add_a_document) is the reference for it. – Darky WC Feb 27 '22 at 10:20
4

I had similar issue, And I thing there were changes at the API of firestore.

I was getting an error for code like :

const postsRef = db.collection('posts').doc(postKey);
batch.set(postsRef, {title: 'Hello Again, World'});

The change I found that was necessary is to take the ref of the doc object:

const postsRef = db.collection('posts').doc(postKey).ref;

I hope this helps you all !

Pini Cheyni
  • 5,073
  • 2
  • 40
  • 58
1

In order to generate the uid automatically beforehand the creation of document, you can make use of createID() function from angularFireAuth as follows:

`

constructor(
    private angularFireStore: AngularFirestore,
   ) {}

 const batch = this.angularFireStore.firestore.batch();

 const autogenUid = this.angularFireStore.createId();
 const collectionReference = this.angularFireStore.collection
                            ('collection_name').doc(autogenUid).ref;

 const docData = {first_field:'value', second_field:'value2'};
 batch.set(collectionReference, docData);
RameshD
  • 912
  • 7
  • 6
  • 1
    thanks for `createId` method reference from Angular Firestore. I remove a reference of `uuid` npm library now. Thanks again :pray: – Pankaj Parkar Feb 18 '23 at 14:53