【问题标题】:Correct Flattened Data Model and noSQL data structure正确的扁平化数据模型和 noSQL 数据结构
【发布时间】:2016-05-08 22:17:37
【问题描述】:

我目前正在使用一项服务构建一个小型应用程序,该服务要求并提倡使用“扁平化”数据模型(该服务是 Firebase)。

由于我之前没有使用数据模型的经验,我想知道我是否以正确的方式展平了我的数据。

这是我开始使用的数据模型模型(它有很多嵌套节点):

// Core data model
    {
        // All registered users 
        "users": {
            // User assigned ID
            "userid": {
                // User information
                "password": "password",
                "email": "test@gmail.com",
                "name": "test",
                // Saved user entries
                "entries": {
                    // Entry ID
                    "entryid": {
                        // Firt part of entry
                        "first": {
                            "one": "yay",
                            "two": "nice",
                            "three": "man"
                        },
                        "second": {
                            "one": "wow",
                            "two": "hmm",
                            "three": "nope"
                        }
                    },

                    "entryid2": {
                        "first": {
                            "one": "kewl",
                            "two": "look",
                            "three": "this"
                       }
                    }
                }
            },
            "id2": {
                // ... and so on
            }
        }
    }

这是我的“扁平化”版本:

// Flattened model
    {
        // All registered users 
        "users": {
            // User assigned ID
            "userid": {
                // User information
                "password": "password",
                "email": "test@gmail.com",
                "name": "test"
            },
            "userid2": {
                // ... and so on
            }
        },
        // All user entries
        "entries": {
            // User assigned UD (entries for particular user)
            "userid": {
                "first": {
                    "one": "yay",
                    "two": "nice",
                    "three": "man"
                },
                "second": {
                    "one": "kewl",
                    "two": "look",
                    "three": "this"
                }
            },
            "userid2": {
                "one": "wow",
                "two": "hmm",
                "three": "nope"
            }
        }
    }

了解这是否是以扁平化方式组织数据的正确(或至少可能)方式将非常有帮助。

【问题讨论】:

    标签: javascript json data-structures firebase data-modeling


    【解决方案1】:

    确实没有 100% 正确的方式来存储数据。它结合了从心理开销的角度对您有用的方法以及您使用的任何商店的技术限制。

    尽管 Firebase 确实提倡“平面数据结构”,但您的任何一个代码示例都远未接近其技术限制。

    来自Understanding Data Docs

    子节点的密钥不能超过 768 字节,也不能超过 32 等级

    无论哪种方式,您都只会深入几个节点,我建议您选择您可以理解和沟通最好的数据模型,因为您不太可能在上面发布的范围内遇到技术限制。

    【讨论】:

    • 感谢您的回答!我知道在当前阶段我没有遇到任何技术限制(将来可能不会遇到这个特定项目)。但是,我唯一担心的是 Firebase 表明使用的两种方法之间存在性能差异。更具体地说,在每次检索节点时,通常(嵌套)方法也会检索其子节点。我应该记住这一点,还是再次强调,它只有在更高的规模上才可行?
    • 如果您使用 Firebase 查询一个节点,您将在生成的快照中获得该节点的所有子节点。如果是大型节点,则需要考虑性能问题。这就是您要解决的问题吗?
    猜你喜欢
    • 2017-04-17
    • 2019-10-05
    • 2019-11-11
    • 2020-03-14
    • 2017-01-07
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多