user623990
user623990

Reputation:

How to organize this setup with MongoDB

I'm coming from a strictly MySQL background and MongoDB is my first contact with NoSQL. Let's say I have a bunch of users and a bunch of groups.

The user schema:

user_id
username
email

The group schema

group_id
name
description

If I was using MySQL and I wanted to have a relationship of each user and the group they belong to, I'd create another table with user_id -> group_id relationships. Would it be proper to create another MongoDB collection for this relationship? If not, what should the scheme look like?

Upvotes: 0

Views: 113

Answers (2)

Gaffe
Gaffe

Reputation: 402

I recommend you take a look at the data modeling documents available on the MondoDB site. The answer to your question will differ depending on the nature of the relationship between a user and a group. You may want to use references or you could store the group info inside the user document.

It could look something like this if you embed it in the user document:

{
   user_id: "the id",
   username: "Something",
   email: "[email protected]",
   group: {
             name: "some name",
             description: "some description"
          }
}

Upvotes: 1

TheZuck
TheZuck

Reputation: 3621

no schema in mongo... the User's group field would either hold an actual group object, or a reference like the group's id.

see this mongo doc.

Upvotes: 1

Related Questions