【问题标题】:Mongoose: Suggested database schemaMongoose:建议的数据库架构
【发布时间】:2011-11-24 15:15:53
【问题描述】:

好的,我来自 mySQL 背景,现在正在尝试使用 nodeJS 和 Mongoose 重建站点。我的旧 mySQL 架构看起来像这样(简化):

users
  user_ID
  user_name
  user_email
  user_password

groups
  group_ID
  group_name
  group_description

groupusers
  groupuser_ID
  group_ID
  user_ID

comments
  comment_ID
  group_ID
  user_ID
  comment_txt

谁能建议重组这个旧的 mySQL 架构以使用 Mongoose 的最佳方法?

【问题讨论】:

    标签: node.js mongodb mongoose nosql


    【解决方案1】:
    users
      user_ID
      user_name
      user_email
      user_password
      Groups
         - grupid 1
         - grupid 2
         - grupid 3
    
    groups
      group_ID
      group_name
      group_description
      comments
        1 - 
           user_ID
           comment_txt
        2 -
           user_ID
           comment_txt 
    

    如果您担心评论大小(目前 mongodb 最大文档大小为 16 mb),您可以将其移至其他文档

    通过这种方式可以找到该组的用户

     db.users.find({Groups:'groupid1'})
    

    还有用户所属的组

       db.users.find({id:userid},{Groups:1})
    

    如果您想使用上述查询检索组相关信息,我建议您将最常访问的组字段也存储在 users.groups 中,像这样

    users
      user_ID
      user_name
      user_email
      user_password
      Groups
         - { grupid 1,name}
         - {grupid 2,name}
         - {grupid 3,name}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      相关资源
      最近更新 更多