开发者

Mongoose: Suggested database schema

Ok so I am coming from a mySQL background and am now trying to rebuild a site using nodeJS and Mongoose. My old mySQL schema looked something like this (simplified):

users
  user_ID
  user_name
  user_email
  user_password

groups
  group_ID
  group_name
  group_des开发者_运维百科cription

groupusers
  groupuser_ID
  group_ID
  user_ID

comments
  comment_ID
  group_ID
  user_ID
  comment_txt

Can anyone suggest the best way to restructure this old mySQL schema to work with 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 

If you are worried about the comment size (currently mongodb max document size is 16 mb), you can move it to other doc

In this way you can find the users of the group by

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

also the groups the user belongs to

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

if you wanted to retrieve the group relevent info with the above query, i suggest you to store most frequently access group fields also with users.groups, like this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜