Updating Field value in multiple documents in same collection in Firebase [duplicate] - android

This question already has answers here:
Can Firestore update multiple documents matching a condition, using one query?
(8 answers)
Closed 2 years ago.
I have created a chat app. In the app there is a collection named Chat and in it there are documents. Each document is a message that was sent and it contains fields such as Message, SentTime, IsButton.
Once something happens in the app (someone clicks a button), I had like to change in all of my documents at that chat the value of IsButton from False to True.
The data looks as follows:
Is there a way to change all of the Field values in one hit instead of using a loop?
I saw there is something called Batch however I'm not sure if it is limited to a maximum 500 updates.
Thank you

The two approaches you've found are pretty much the way to update a bunch of document.
You can:
either update each document individually, which is not nearly as slow as you may think.
or you can update the documents in one of more batches.
Having to update many documents with the same value may be a sign that you should reconsider your data structure. For example, maybe you can store the IsButton in a single, separate document that all clients then read/listen to.

Related

How to handle foreign key kind of situation in google Firestore?

As there is no functionality of foreign Key in Firestore like that of MYSQL, so I am not able to replicate one of my important functionality that is to update a file in one place and it will reflect in every place. Also, Firebase has no functionality to update all the document's specific filed at once.
There are already these kinds of questions but I could not get my solution. Suppose I have a million documents containing a filed which is the density of a material. Later on, I found that my density value was wrong so how to update that value in all documents efficiently. Also, I do not want to use server/admin SDK.
If you need to change the contents of 1 million documents, then you will need to query for those 1 million documents, iterate the results, then update each of those 1 million documents individually.
There is no equivalent of a sql "update where" statement that updates multiple documents in one query. It requires one update per document.
If don't want to use the Admin SDK, then the option that you have is to update the value of your densityMaterial property on the client, which might not be the best solution. However, if you can divide the update operation in smaller chunks, you might succeed.
If you are using a POJO class to map each document, then you might be interested in my answer from the following post:
How to update one field from all documents using POJO in Firestore?
And if you are not using a POJO class, please check my answer from the following post:
Firestore firebase Android search and update query
Regarding the cost, you'll be billed with one write operation for every document that is updated. If all 1 MIL documents will be updated, then you'll be billed with 1 MIL write operations.
Edit:
Suppose I have a million documents containing a filed which is the density of a material. Later on, I found that my density value was wrong so how to update that value in all documents efficiently.
If all of those 1 MIL documents contain a property called densityMaterial, that holds the exact same value, it doesn't make any sense to store that property within each document. You can create a single document that contains that particular value, and in each and every document of those 1 MIL, simply add only a reference to that document. A DocumentReference is a supported data-type. Now, if you need to change that value, it will incur only a single document write.
However, if you have different values for the densityMaterial property and all of them are wrong, then you don't have a problem with the database, you have a problem with the mechanism/people that are adding data. It's not a matter of a database problem if you have added 1 MIL incorrect documents.
Why not chose MySQL?
MySQL cannot scale in the way Cloud Firestore does. Firestore simply scales massively.
Can I avoid this problem anyhow?
Yes, you can buy using a single document for such details.

What is the best practice to sequentially increased field in Firestore? [duplicate]

This question already has an answer here:
Firestore and Auto Increment Ids
(1 answer)
Closed 3 years ago.
I use Google Firestore as database in my project. It creates random generated id's for document names. I understood it's made with an purpose and I must use timestamp as a field in my document to order them by time they created.
On the other hand I also need a field that increase sequentially every time a new object added to the database. And I need to show customers this field because it will contain Order Id.
What is the best practice and how to create a field sequentially increased every time a new object created. How to manually increase new item realted field?
Sorry if it is an easy question, I am just lost. Any help is appreciated.
Seeing that you want a sequential order ID, you can probably maintain a document that stores a counter of the latest order ID.
Every time you want to add a new document, increment the order ID and use it to store it in the new document key or data.
As of now it is not possible to do the increment and at the same time read the latest value in a single operation. You would want to do it in a Firestore Transaction as explained in the answer of another question here. I suggest you read the question and the other answers in that thread as they are useful.

How to filter collections in Firebase Firestore? [duplicate]

This question already has answers here:
Google Firestore - How to get several documents by multiple ids in one round-trip?
(17 answers)
Closed 4 years ago.
I have below FireStore data
And I have a list specialUsers which is defined as
val specialUsers = arrayOf("stark#gmail.com", "lannester#gmail.com")
So I want to get collections which are in the specialUsers, something like
collection("users").whereIn(specialUsers)
But, couldn't find any documentation related to this. How can I perform above intention in Firestore?
As he says here in 6:19
There is still no way for the database to automatically grab specific user name and profile for each review as I requesting them. I would need to make separate database request for every single review I get to fetch this information and that's bad. so If we wont to automatically include information about who rote a particular review we will need to copy sample of the user profile to the particular review and this is the way (to brake data Normalization) specifically in could FireStore.

ANDROID FIRESTORE : get all the documents that has "current user" in it's sub collection [duplicate]

This question already has an answer here:
How to query a Firestore collection using a property from a step deeper with Kotlin? [duplicate]
(1 answer)
Closed 4 years ago.
I am new to Firestore cloud and I am trying to learn about it.
I am trying to build a project and I am stuck at one point.
My Firestore database looks like this:
enter image description here
enter image description here
I have a Groups collection that has "Users List" Sub-collection" where I have "timestamp" and "user_id".
When I created a group I grabbed the user_id of the "user" that creates a group and added that in "Users List".
Now I am looking to retrieve all the "Groups"(groupName) that the "CurrentUser" is involved in, and I am having hard time to figure it out because I found that NoSQL databases are very different from SQL databases.
Would be great if someone can shed a light on this matter.
Thank you
I'm not sure about this, but I think your best bet is to add some field to collect group that each user involve in "Users" Collection using array or object to collect it since Firestore can collect array or object.
But TBH, I not sure this is the best, but it is the best I can think rightnow. Because with these you only need to query 1 time for each user that you want to get usergroup and it help you to not use lots of query to get usergroups for each user. But it will couse you some additional size. But it still fine since Firestore costing is base on both query and size for this case. Little(?) additional data is better than waste lots of query to get usergroup for only one user.

how to make Range filters on multiple field in Firebase Firestore? [duplicate]

This question already has answers here:
Firestore multiple range query
(3 answers)
Closed 3 years ago.
I know that basically performing range filters on multiple fields like the code below is not allowed:
eventRef
.whereField("dateTimeStart", isGreaterThan: startTime)
.whereField("dateTimeStart", isLessThan: finishTime)
.whereField("price", isGreaterThan: 10)
.whereField("price", isLessThan: 1000)
I have to choose between performing filter based on "dateTimeStart" or just "price" in one query, but I need to filter based on those two criteria, is there any trick to do this? I just can think that I have to do query based on price only and then the result of the query need to be sorted manually
Is there any better option than this approach?
When you are using the query in your code, you'll get for sure an error that sounds like this:
Caused by: java.lang.IllegalArgumentException: All where filters other than whereEqualTo() must be on the same field. But you have filters on 'dateTimeStart' and 'price'
So there is no way in Firestore to use whereEqualTo() function on more than one property. In this case, you should consider augmenting your data structure to allow a reverse lookup. So you should duplicate data by creating a new collection named priceAndPeriod in which you should add all your products that have the price between 10 and 1000 and also have the dateTimeStart between startTime and finishTime.
Note, there is no problem with duplicating data, when it comes to Firebase. This is a quite common practice, which is named denormalization and for that, I recommend you see this video, Denormalization is normal with the Firebase Database. It is for Firebase Real-time database but same principle applies to Cloud Firestore.
When you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.

Categories

Resources