添加新字段或更改所有Firestore文档的结构 [英] Add new field or change the structure on all Firestore documents

查看:98
本文介绍了添加新字段或更改所有Firestore文档的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个个用户的集合。集合中的每个文档都有名称电子邮件作为字段。

Consider a collection of users. Each document in the collection has name and email as fields.

{
  "users": {
    "uid1": {
      "name": "Alex Saveau",
      "email": "saveau.alexandre@gmail.com"
    },
    "uid2": { ... },
    "uid3": { ... }
  }
}

现在考虑一下,通过这种有效的Cloud Firestore数据库结构,我启动了第一个移动应用程序。然后,在某个时候,我意识到我想包含另一个字段,例如 last_login

Consider now that with this working Cloud Firestore database structure I launch my first version of a mobile application. Then, at some point I realize I want to include another field such as last_login.

在代码中,阅读

FirebaseFirestore.getInstance().collection("users").get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        mUsers.add(document.toObject(User.class));
                    }
                }
            }
        });

其中类 User 现在包含名称电子邮件 last_login

由于新的 User 字段( last_login )不包含在存储的旧用户中在数据库中,应用程序崩溃,因为新的 User 类期望一个 last_login 字段,该字段返回为 null 通过 get()方法。

Since the new User field (last_login) is not included in the old users stored in the DB, the application is crashing because the new User class is expecting a last_login field which is returned as null by the get() method.

会是什么最佳做法是将 last_login 包括在数据库的所有现有 User 文档中,而不会丢失其新版本的数据该应用程序?我应该只运行一个代码段来完成此任务,还是有解决这个问题的更好方法?

What would be the best practice to include last_login in all the existing User documents of the DB without losing their data on a new version of the app? Should I run an snippet just once to do this task or are there any better approaches to the problem?

推荐答案

NOSQL数据库的差距:面向文档的数据库不能保证数据的结构完整性(就像RDBMS一样)

You fell into a gap of NOSQL databases: Document oriented databases do not guarantee structural integrity of the data (as RDBMS do)

交易是:


    RDBMS 中的所有
  • 在任何给定时间(在同一实例或群集中)都具有相同的结构。更改结构(ER图)时,您必须迁移所有现有记录的数据,这会花费很多时间和精力。

  • in an RDBMS all stored data have the same structure at any given time (within the same instance or cluster). When changing the structure (ER-diagram) you have to migrate the data for all existing records which costs time and effort.

结果是可以优化应用程序

In result you application can be optimised for the current version of the data structure.

在面向面向文档的数据库中的每个记录都是独立的页面具有自己的独立结构。如果更改结构,则仅适用于 new 文档。因此,您不需要迁移现有数据。

in a Document oriented database each record is an independent "Page" with its own independent structure. If you change the structure it only applies to new documents. So you don't need to migrate the existing data.

因此,您的应用程序必须能够处理当前数据库中曾经使用过的数据结构的所有版本。

In result you application must be able to deal with all versions of your data structure you ever used in your current database.

我不了解Firebase的详细信息,但总的来说,您永远不会 update NOSQL数据库。您只能创建文档的新版本。因此,即使您更新了所有文档,您的应用程序也必须准备好处理旧文档。数据结构...

I don't know about firebase in detail but in general you never update a document in a NOSQL database. You only create a new version of the document. So even if you update all documents your application must be prepared to deal with the "old" data structure...

这篇关于添加新字段或更改所有Firestore文档的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆