aggregation framework - Three Way Many-to-Many in MongoDb -
i have seen plenty of examples on internet , answers on over various ways establish many-to-many relation between 2 entities. example of users
, roles
used. problem quite straight forward , simple understand me has 2 months experience mongodb , half of time has been spent fighting against rdbms schema patterns aren't necessary in mongodb.
however, little lost trying establish 3 way many-to-many relation seems straight forward in rdbms world i'm unable think through how done in mongodb.
here's example.
let's have 3 entities: project
, user
, projectrole
.
each user
can have multiple projects
, each project
can have multiple users
.
each user
can have multiple projectroles
, each projectrole
can have multiple users
.
another condition projectroles
can assigned users
in relation project
. is, users
can't have projectrole
if they're not part of project
.
similarly, projectroles
related projects
via users
.
so there ways have thought creating these relationships of these (or maybe all) ways direct result of rdbms way of doing things.
1.
create join
collection these 3 entities such each document looks this:
{ "user_id" : objectid("someuserid"), "project_id" : objectid("someprojectid"), "projectrole_id" : objectid("someprojectroleid") }
2.
copy on user
(embed) object project
document projectrole object/information embedded, this:
{ "name" : "projectname", "users" : [{ "username" : "firstusername", "projectrole" : "somerole" }, { "username" : "secondusername", "projectrole": "someotherrole"}] }
3.
copy on (embed) project
object in user
document associated projectrole
user
in project
, this:
{ "name" : "username", "projects" : [{ "projectname" : "someproject", "projectrole" : "somerole"}, { "projectname" : "someotherproject", "projectrole" : "someotherrole"}] }
option 1 has best chance of me being able keep data clean , keep integrity intact. going pain read since goes on 3 collections (and possibly more project gets more complex).
option 2 , 3 seem, me @ least, mongodb style embedded information. prefer 2 on 3 because projects
sort of higher in hierarchy followed users
. in other words, theoretically each project
have lot more users
each user
, number of projects
have been assigned.
problem option 2 data integrity. can dealt making sure on each update, example, of user
server goes , searches through project
collection , updates instances of user
object (matching on id). potentially extremely slow operation (perhaps indexing on userid
make faster) priority read-speed rather quick updates. assumption user
data remain unchanged on period of time (on other hand project
data updated quite frequently).
past point i'm not entirely sure how proceed. @ least on right track or solutions based on fundamental misunderstanding of either mongodb or actual problem itself?
Comments
Post a Comment