Modeling a good Mongodb schema really depends on how you access your data. In this case, you will index your member. User_id that looks ok. But the size of your document will grow as you add viewers, editors and administrators. In addition, your schema will make queries more difficult:
Request projects, where user_id xxx is the editor:
Again, you may not need to request such projects, so your schema looks fine. But if you need to request your projects using user_id AND role, I would recommend that you create a collection of "project_membership":
db.project_memberships.insert( { "project_id" : ObjectId("4d730fcfcedc351d67000032"), "editors" : [ ObjectId("4d730fcfcedc351d67000002"), ObjectId("4d730fcfcedc351d67000004") ], "viewers" : [ ObjectId("4d730fcfcedc351d67000002"), ObjectId("4d730fcfcedc351d67000004"), ObjectId("4d730fcfcedc351d67000001"), ObjectId("4d730fcfcedc351d67000005") ], "administrator" : [ ObjectId("4d730fcfcedc351d67000011"), ObjectId("4d730fcfcedc351d67000012") ] } ) db.project_memberships.ensureIndex({"editors": 1}) db.project_memberships.ensureIndex({"viewers": 1}) db.project_memberships.ensureIndex({"administrator": 1})
Or even simpler ... add an index to the project diagram:
db.projects.ensureIndex({"memberships.role": 1})
source share