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,767
  • 2
  • 30
  • 52

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
  • 130,605
  • 17
  • 163
  • 193
  • 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