I'm sorry, but I'm new to Mongodb. I can not understand how to design the next model. Collection of users: [ {name:'user1', password:'123', groups:['all_users','author','forum_admin']}, {name:'user2', password:'123', groups:['all_users','author',]}, {name:'user3', password:'123', groups:['anonim']} ] Hierarchy of user groups: [ { _id:'all_users', name:'All users', groups:[ {_id:'author', name:'Author'} {_id:'anonim', name:'Anonimous user'} { _id:'admin', name:'Administrator', groups:[ {_id:'sa', name:'Super administrator'}, {_id:'sys_admin', name:'System administrator'}, {_id:'forum_admin', name:'Forum administrator'} ] } ] } ]
How can I associate users and groups? I tried to add in each user a list of groups. But I have a problem. *H**ow to extract the group information from the group hierarchy by them _id?* Request db.groups.find({_id:'forum_admin'}) return empty list.
Just to clarify: Does your groups collection only consist of a single document where the _id is 'all_users' ? Are you embedding the hierarchy of all groups into that single document? It looks like that from what you stated as your Hierarchy of user groups. That would also explain why you get an empty result when you query for a group with the _id of "forum_admin", because there is no such document that has that _id on the top level.
There are several ways to store a group hierarchy in MongoDB:
- You can use that one-document approach, but then you need to pull the complete document to the client side and search the tree for a certain subgroup within your application. - You can insert one document for each group into the groups collection. If you want to keep the hierarchy information, you can add a "parent" field that points to the parent group. - As above, but instead of pointing to the parent, you can keep the tree of sub-groups as a "children" sub-document.
It really depends on what you're planning to do exactly and how your use case is for the groups. Think about the different situations when you need access to the groups collection and what answers you want to find. For example, do you really need the hierarchy every time you query the groups collection? Or do you mostly just want to get some extra information or find certain rights of a user's group? There is no one single right answer, it really depends what you're trying to achieve.
On Sunday, November 18, 2012 3:26:34 AM UTC+11, Andrey wrote:
> I'm sorry, but I'm new to Mongodb. I can not understand how to design the > next model. > Collection of users: > [ > {name:'user1', password:'123', > groups:['all_users','author','forum_admin']}, > {name:'user2', password:'123', groups:['all_users','author',]}, > {name:'user3', password:'123', groups:['anonim']} > ] > Hierarchy of user groups: > [ > { _id:'all_users', > name:'All users', > groups:[ > {_id:'author', name:'Author'} > {_id:'anonim', name:'Anonimous user'} > { _id:'admin', > name:'Administrator', > groups:[ > {_id:'sa', name:'Super administrator'}, > {_id:'sys_admin', name:'System administrator'}, > {_id:'forum_admin', name:'Forum administrator'} > ] > } > ] > } > ]
> How can I associate users and groups? I tried to add in each user a list > of groups. But I have a problem. *H**ow to extract the group information > from the group hierarchy by them _id?* Request > db.groups.find({_id:'forum_admin'}) return empty list.
Thank you for your answer. I decided to use the second method (which is very similar to the implementation of a relational database). I just thought that Mongo can do it somehow more elegant.