skip to Main Content

Suppose I have a users collection. The users collection has a large number of documents in it. Now in my app, I have a feature request that forces me to add or remove a field in my users collection data model. How can I add a new field or remove an existing field from all my users documents? Is there any best practice that the community recommends here?

2

Answers


  1. As I understood, You can do the following thing

    1. Add New Field

    If you are using Firebase Functions- you can create one function and write an update query with a new field and set one default value and Run the function. You can do the same from android also with kotlin/java.

    2. Remove existing Field

    If you are using Firebase Functions- you can create one function and write a query to delete one field and Run the function. You can do the same from android also with kotlin/java.

    Look for a better approach If any, Its suggestion as per my knowledge.

    Login or Signup to reply.
  2. How can I add a new field or remove an existing field from all my users documents?

    While @AdityaNandardhane solution might work, please note that if you have a lot of documents, then you have a lot of update operations to perform, which also means that you have to play a lot of writes.

    So the best approach would be to perform the update, only when the user reads the document. When it comes to users, most likely the details of the users are displayed on a profile screen. This means that when the users want to check the profile, before displaying the data, check for the existence of the new field. If it doesn’t exist, then perform the update operation, and right after that display the data, otherwise, just display the data. This means that you’ll have to pay for an update operation only when needed. It doesn’t make any sense to update all documents, of all users, since there may be users that will never use their accounts anymore. So there is no need to pay for them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search