How To Save A Document With A Dynamic Id Into Cloud Firestore? Always Changing
Solution 1:
If I understood correctly you are adding data to the Cloud Firestore and each new document will have as name an incremental number.
If you query all the documents and then count how many are of them, then you are going to end up with many document reads as the database increases. Don't forget that Cloud Firestore is charging per document Read and Write, therefore if you have 100 documents and you want to add new document with ID: 101, then with the approach of first reading all of them and then counting them will cost you 100 Reads and then 1 Write. The next time it will cost you 101 Reads and 1 Write. And it will go on as your database increases.
The way I see is from two different approaches:
Approach 1:
You can have a single document that will hold all the information of the database and what the next name should be.
e.g.
The structure of the database:esequiz:0:last_document:21:question:"What is 3+3?
right: "6"
wrong: "0"
2:
question: "Whatis2+3?right:"5"wrong:"0"
So the process will go as follows:
- Read document "/esequiz/0" Counts as 1 READ
- Create new document with ID:
last_document + 1
Counts as 1 WRITE - Update the document that holds the information:
last_document = 3;
Counts as 1 WRITE
This approach cost you 1 READ and 2 WRITES to the database.
Approach 2:
You can load only the last document from the database and get it's ID.
e.g.
The structure of the database (Same as before, but without the additional doc):
esequiz:
1:
question: "What is 3+3?
right: "6"
wrong: "0"
2:
question: "What is2+3?
right: "5"
wrong: "0"
So the process will go as follows:
- Read the last document using the approach described in Order and limit data with Cloud Firestore documentation. So you can use
direction=firestore.Query.DESCENDING
with combination oflimit(1)
which will give you the last document. Counts as 1 READ - Now you know the ID of the loaded document so you can create new document with ID: that will use the loaded value and increase it by 1. Counts as 1 WRITE
This approach cost you 1 READ and 1 WRITE in total to the database.
I hope that this information was helpful and it resolves your issue. Currently counting documents in Cloud Firestore is not supported.
UPDATE
In order for the sorting to work, you will also have to include the id as a filed of the document that so you can be able to order based on it. I have tested the following example and it is working for me:
Structure of database:
esequiz:
1:
id: 1
question: "What is 3+3?
right: "6"wrong: "0"2:
id:2question: "What is 2+3?
right: "5"wrong: "0"
As you can see the ID is set the same as the document's ID.
Now you can query all the documents and order based on that filed. At the same time you can only retrieve the last document from the query:
const {Firestore} = require('@google-cloud/firestore');
const firestore = new Firestore();
async functiongetLastDocument(){
let query = firestore.collection('esequiz');
query.orderBy('id', 'desc').limit(1).get().then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
console.log(`Found document at ${documentSnapshot.ref.path}`);
console.log(`Document's ID: ${documentSnapshot.id}`);
});
});
}
OUTPUT:
Found document at esequiz/2
Document's ID: 2
Then you can take the ID and increase it by 1 to generate the name for your new document!
UPDATE 2
So, the initial question is about "How to store data in the Cloud Firestore with documents having incremental ID", at the moment you are facing issues of setting up Firestore with you project. Unfortunately, the new raised questions should be discussed in another Stackoverflow post as they have nothing to do with the logic of having incremental IDs for the document and it is better to keep one issue per question, to give better community support for members that are looking for a solution about particular issues. Therefore, I will try to help you, in this post, to execute a simple Node.js script and resolve the initial issue, which is storing to Cloud Firestore documents with incremental IDs. Everything else, on how to setup this in your project and how to have this function in your page, should be addressed in additional question, where you also will need to provide as much information as possible about the Framework you are using, the project setup etc.
So, lets make a simple app.js work with the logic described above:
- Since you have Cloud Firestore already working, this means that you already have Google Cloud Platform project (where the Firestore relies) and the proper APIs already enabled. Otherwise it wouldn't be working.
- Your guide in this tutorial is the Cloud Firestore: Node.js Client documentation. It will help you to understand all the methods you can use with the Firestore Node.js API. You can find helpful links for adding, reading, querying documents and many more operations. (I will post entire working code later in this steps. I just shared the link so you know where to look for additional features)
- Go to Google Cloud Console Dashboard page. You should login with your Google account where your project with the Firestore database is setup.
- On top right corner you should see 4 buttons and your profile picture. The first button is the Activate Cloud Shell. This will open a terminal on the bottom of the page with linux OS and Google Cloud SDK already install. There you can interact with your resources within GCP projects and test your code locally before using it in your projects.
- After clicking that button, you will notice that the terminal will open in the bottom of your page.
- To make sure that you are properly authenticated we will set up the project and authenticate the account again, even if it is already done by default. So first execute
$ gcloud auth login
- On the prompted question type
Y
and hit enter - Click on the generated link and authenticate your account on the prompted window
- Copy the generated string back to the terminal and hit enter. Now you should be properly authenticated.
- Then setup the project that contains Cloud Firestore database with the following command:
$ gcloud config set project PROJECT_ID
. Now you are ready to build a simple app.js script and execute it. - Create a new app.js file:
nano app.js
- Inside paste my code example that can be found in this GitHub link. It contains fully working example and many comments explaining each part therefore it is better that it is shared through GitHub link and not pasted here. Without doing any modifications, this code will execute exactly what you are trying to do. I have tested it my self and it is working.
- Execute the script as:
node app.js
- This will give you the following error:
Error: Cannot find module '@google-cloud/firestore'
Since we are importing the library @google-cloud/firestore
but haven't installed it yet.
- Install
@google-cloud/firestore
library as follows:$ npm i @google-cloud/firestore
. Described in DOC. - Execute the script again:
$ node app.js
. - You should see e.g.
Document with ID: 3 is written.
- If you execute again, you should see e.g.
Document with ID: 4 is written.
All those changes should appear in your Cloud Firestore database as well. As you can see it is loading the ID of the last document, it is creating a new ID and then it creates a new document with the given arguments, while using the new generated ID as document name. This is exactly what the initial issue was about.
So I have shared with you the full code that works and does exactly what you are trying to do. Unfortunately, the other newly raised issues, should be addressed in another Stackoverflow post, as they have nothing to do with the initial issue, which is "How to create documents with incremental ID". I recommend you to follow the steps and have a working example and then try to implement the logic to your project. However, if you are still facing any issues with how to setup Firestore in your project then you can ask another question. After that you can combine both solutions and you will have working app!
Good luck!
Solution 2:
I don't think the way you are trying to get the length of the collection is right and I am entirely not sure what is the best way to get that either. Because the method you are trying to implement will cost you a lot more as you are trying to read all the records of the collection.
But there can be alternatives to get the number you require.
- Start storing the ID in the record and make the query with limit 1 and a descending sort on ID.
- Store the latest number in another collection and increment that every time you create a new record, And fetch the same whenever needed.
These methods might fail if concurrent requests are being made without transactions.
Post a Comment for "How To Save A Document With A Dynamic Id Into Cloud Firestore? Always Changing"