我的数据库架构适合与 Mongo 一起使用吗?

发布于 2024-10-11 12:53:43 字数 1049 浏览 4 评论 0原文

太长了;博士 我的架构还好吗?

我今年的决心是学习一些新的东西,我选择学习一些关于非相关数据库的知识,即 Mongo。我目前正在开发一个简单的应用程序,它使用 mongo 作为数据库引擎。

我正在开发的应用程序是一个简单的问卷应用程序:管理员创建问题,(登录)用户回答它们。所以:用户有很多答案属于问题。 对于这样的应用程序最合适的架构是什么?我自己创建了下一个架构(伪),但我想知道您是否有任何解决此问题的提示/技巧。

users [
    {
        # some required fields to authenticate the user
        email: [email protected],
        password: ...
        etc.

        # next fields are this users answers to a question
        # the key is the question's id, it's value the answer
        1: 'John',
        2: 'Doe',
    },
]

questions [
    { # no 1 (I know id's in mongo aren't numbered this way,
      # this is just for the sake of readability.
        question: What is your first name?,
        type: open,
        required: true,
    },
    { # no 2
        question: What is your last name?,
        type: open,
        required: false,
    },
    # and so on.
]

tl;dr
Is my schema okay?

My resolution for this year is to learn something new, and I've chosen to learn something about non-rel databases, i.e. Mongo. I'm currently working on a simple app which uses mongo as the database engine.

The application I'm working on is a simple questionnaire app: admin creates question, (logged in) user answers them. So: User has many Answer belongs to Question. What would be the most appropriate schema for such an app? I created the next schema myself (pseudo), but I'm wondering if you have any hints/tips solving this.

users [
    {
        # some required fields to authenticate the user
        email: [email protected],
        password: ...
        etc.

        # next fields are this users answers to a question
        # the key is the question's id, it's value the answer
        1: 'John',
        2: 'Doe',
    },
]

questions [
    { # no 1 (I know id's in mongo aren't numbered this way,
      # this is just for the sake of readability.
        question: What is your first name?,
        type: open,
        required: true,
    },
    { # no 2
        question: What is your last name?,
        type: open,
        required: false,
    },
    # and so on.
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-10-18 12:53:43

我会将答案移至问题集合中:

{_id: 1, 
 question: "?", 
 type: "open", 
 required: true, 
 answered: [
   {email: "[email protected]", answer: "John"},
   {email: "[email protected]", answer: "Bob"},
 ]
}

此外,使用动态字段(如答案 ID)将使您无法对它们建立索引。

I'd move answers inside questions collection:

{_id: 1, 
 question: "?", 
 type: "open", 
 required: true, 
 answered: [
   {email: "[email protected]", answer: "John"},
   {email: "[email protected]", answer: "Bob"},
 ]
}

Also using dynamic fields (like answer IDs) will make you impossible to index them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文