Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of .push in Firestore?

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()
like image 901
Jojo Narte Avatar asked Jun 19 '18 07:06

Jojo Narte


People also ask

What is a Subcollection in firestore?

A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries. You can create a subcollection called messages for every room document in your rooms collection: collections_bookmark rooms.


1 Answers

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.

like image 174
Alex Mamo Avatar answered Nov 15 '22 08:11

Alex Mamo