Rafa
Rafa

Reputation: 153

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.

Upvotes: 3

Views: 848

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138834

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.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317497

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.

Upvotes: 2

Related Questions