Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_description

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?

like image 929
wilsonpage Avatar asked Sep 25 '11 13:09

wilsonpage


1 Answers

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}
like image 70
RameshVel Avatar answered Sep 28 '22 18:09

RameshVel