6

I am trying to convert my firebase implementation which previously used realtime database to use firestore as I like the idea of collections and the perks of using it.

How do I implement below into firestore equivalent?

firebase.database().ref('documentPath').push()
Jojo Narte
  • 2,377
  • 1
  • 24
  • 49

1 Answers1

12

To have the same bahaviour in Cloud Firestore as you have in Firebase Realtime database when using the push() function, is to let Cloud Firestore auto-generate an ID for you. You can do this by calling add() function like this:

var addYourDoc = db.collection('documentPath').add({
  property_key: 'property_value',
}).then(ref => {
  console.log('document ID: ', ref.id);
});

The output in the console will be the actual generated id.

Alex Mamo
  • 112,184
  • 14
  • 139
  • 167
  • it did. I actually only have to just set the path then add the property then firebase will handle it for me. – Jojo Narte Jun 21 '18 at 08:55