简体   繁体   English

模型Firestore数据

[英]Model Firestore Data

I am creating a new database using Firestore. 我正在使用Firestore创建一个新数据库。 I am new to NoSQL and trying to determine best practices for modeling my data. 我是NoSQL的新手,正在尝试确定对数据建模的最佳实践。 I know that Firestore data is shallow by default (as opposed to the Realtime Database) so nesting isn't a problem. 我知道默认情况下,Firestore数据较浅(与实时数据库相对),因此嵌套不是问题。 That said, what would be the best way to model a more-or-less standard user object? 也就是说,建模或多或少的标准用户对象的最佳方法是什么?

Option 1 — Parent Level Only: 选项1-仅限家长级别:

users {
    uid {
        name: 'Bob',
        officeNumber: 1234567890,
        faxNumber: 0987654321,
        email: 'test@test.com',
        domain: '@test.com',
        facebook: 'bobdylan97',
        twitter: 'bobbystwitter',
        instagram: 'bobinsta',
        street: '111 N Elm St',
        city: 'Brooklyn',
        state: 'NY',
        zip: 12345,
        height: 72,
        weight: 200,
        hairColor: 'brown',
        eyeColor: 'blue'
    }
}

Option 2 — Nest Multiple Levels: 选项2-嵌套多个级别:

users {
    uid {
        personal {
            name {
                first: 'Bob',
                last: 'Dylan'
            },
            attributes {
                height: 72,
                weight: 200,
                hairColor: 'brown',
                eyeColor: 'blue'
            }
        },
        contact {
            phone {
                office: 1234567890,
                fax: 0987654321
            },
            email: 'test@test.com',
            domain: '@test.com',
            social {
                facebook: 'bobdylan97',
                twitter: 'bobbystwitter',
                instagram: 'bobinsta'
            }
        },
        address {
            street: '111 N Elm St',
            city: 'Brooklyn',
            state: 'NY',
            zip: 12345
        }
    }
}

The company I am building this for is growing and there may be additional data added at various points, so scaling is a potential concern. 我正在为此服务的公司正在增长,并且可能会在各个时间点添加其他数据,因此扩展是一个潜在的问题。 Are there any problems or concerns with grouping data like in Option 2? 是否像选项2一样对数据分组有任何问题或担忧? What is the best practice for modeling data like this? 像这样的数据建模的最佳实践是什么? Is the best practice to optimize the model for querying or for organization? 是针对查询或组织优化模型的最佳实践吗?

I would reccomend you to watch: What is a NoSQL Database? 我建议您注意: 什么是NoSQL数据库? - Get to Know Cloud Firestore Ep.1 -了解Cloud Firestore第1版

It perfectly explains the basic concepts of Firestore. 它完美地解释了Firestore的基本概念。

If you are storing users in a users collection and the uid is the document ID, then Option 2 should work fine for you. 如果将用户存储在users集合中,并且uid是文档ID,则选项2应该适合您。

You are only storing a fairly small amount of data about each user in a document, so you shouldn't hit the 1Mb limit. 您仅在文档中存储有关每个用户的相当少量的数据,因此您不应达到1Mb的限制。 You could probably squeeze a base64 encoded profile picture in here, too, without problem. 您也可以在这里毫无问题地压缩base64编码的个人资料图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM