好的,我来自 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 的最佳方法?
最佳答案
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}
关于node.js - Mongoose:建议的数据库架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7545754/