Cloud Firestore

Learn how to perform CRUDL operations with Cloud Firestore

Start with adding Cloud Firestore as your data source.

List data

Open the project and create a new Action. Choose the Code action step and insert a code that will load your Firestore collection.

const collection = await firebase.firestore().collection("collectionName").get();
const result = [];
collection.forEach(doc => {
    const data = doc.data();
    result.push({ id: doc.id, ...data });
});
return result;

Then, press the Run Action button and open the Result section. You must have your collection's data loaded there.

Read a document

To fetch data about a certain document, start with Creating a new Action and selecting the Code step.

Insert the code below to read the document's data:

const doc = await firebase.firestore().collection('collectionName').doc('docID').get();
return doc.data();

Press the Run Action button and open the Result section. You will see the document's data.

Create a new record

Let's create a new record in Firestore. First of all, we need to create a new Action. Choose a Code action type and insert the code that will create a new record in your Firestore.

const newUser = { name: "Nik" };
const doc = await firebase.firestore().collection("collectionName").add(newUser);
return doc.id;

Press the Run Action button and open the Result section. You must have a new record there.

Delete a record

Let's delete a record from Cloud Firestore. Create a new action and select a Code action step. Type in a code:

await firebase.firestore().collection('collectionName').doc('docID').delete(); 

Press the Run Action button and open the Result section to check that the record has been deleted.

Update a record

You can update the data in Cloud Firestore with 2 methods: set() and update(). The difference is, set() will overwrite the whole document.update()will update only certain fields that need to be updated. Let's check both of the approaches for updating your Firestore data.

Create a new action and selecta Code action type. The code would be as follows:

set()

const doc = await firebase.firestore().collection('collectionName').doc('docID').set({
    docField: newFieldValue
});

In case you do not want to overwrite your data, you can add merge: true to your code, so that you only update the required fields

update()

For the update() method, type in the below Code:

const doc = await firebase.firestore().collection('collectionName').doc('docID').update({
    docField: newFieldValue
});

Click Run Action and check the Result section for the outcome.

All the other operations with the Firestore collections like Filter, Order can be done via the Firestore API. You can find an API reference here.

More useful articles on how to further manage your data:

Last updated