I've started to implement a full MEAN-stack application (MongoDB, Express, Angular2, node) and I am struggling with the data design for the access management in the MongoDB. In my old thinking tracks, I have a potential relational solution in SQL, but I want to design it with MongoDB, if possible.

Here is some mongoose-Code on the schemas:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var AccessGroupSchema = new Schema({
    name : { type: String, required: true, unique: true },
    projects : Schema.Types.Mixed,
    roles : [String],
    state : Number,
/* projects:
    { prjid1 : { acl: [String],
                 sub: {
                     subprj1: { acl: [] },
                     subprj2: { acl: [] },
       prjid2 : { acl: [String],
                 sub: {
                     subprj1: { acl: [] },
                     subprj2: { acl: [] },
var AccessUserSchema = new Schema({
    userID: { type: Schema.Types.ObjectId, ref: 'User', unique:true },
    created_at: Date,
    updated_at: Date,
    state : Number, // currently ignored.
    groups : [{ type: Schema.Types.ObjectId, ref: 'AccessGroup' }],

I've written some statics and methods for the AccessUser and AccessGroup, but as I tried to implement the requirement "for a given user list me all projects with rights to project and subprojects per line", I failed. My potential solution in SL looks like this:

Table1: "accessgrp2sub": groupid, subprojectid, acl (Bitfield integer)
Table2: "users2grp": userid, groupid
Table3: "subprojects": subprojectid,projectid,subtitle
Tables: other tables like user,projects, groups...

Is there a design pattern for implementing this relation pattern in MongoDB? I've experimented a lot with the mixed data type in schemas and also with the population mechanism between two or more documents. Currently I do not see a solution. Any help is appreciated.

  • 1
    It seems to me your data is relational. MongoDB is the the wrong solution for your needs. – Greg Burghardt Mar 7 '17 at 19:53
  • You may want to look into how to manage many-to-many relationaships in Mongodb: stackoverflow.com/questions/2336700/…\ – JeffO Mar 7 '17 at 22:44
  • Why doesn't work? What have you tried and failed? What query? – Constantin Galbenu Mar 8 '17 at 21:43
  • I failed to formulate the query. With populate and a complex json query I didn't manage to get a compressed list of subprojects, which are belonging to the user. – Myonara Mar 9 '17 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.