balexand
balexand

Reputation: 9675

Modeling many-to-many :through with Mongoid/MongoDB

I'm relatively new to Mongoid/MongoDB and I have a question about how to model a specific many-to-many relationship.

I have a User model and a Project model. Users can belong to many projects, and each project membership includes one role (eg. "administrator", "editor", or "viewer"). If I were using ActiveRecord then I'd set up a many-to-many association between User and Project using has_many :through and then I'd put a field for role in the join table.

What is a good way to model this scenario in MongoDB and how would I declare that model with Mongoid? The example below seems like a good way to model this, but I don't know how to elegantly declare the relational association between User and the embedded ProjectMembership with Mongoid.

Thanks in advance!

db.projects.insert(
  { 
    "name" : "Test project",
    "memberships" : [
      {
        "user_id" : ObjectId("4d730fcfcedc351d67000002"),
        "role" : "administrator"
      },
      {
        "role" : "editor",
        "user_id" : ObjectId("4d731fe3cedc351fa7000002")
      }
    ]
  }
)

db.projects.ensureIndex({"memberships.user_id": 1})

Upvotes: 4

Views: 1853

Answers (1)

Manu Eidenberger
Manu Eidenberger

Reputation: 2096

Modeling a good Mongodb schema really depends on how you access your data. In your described case, you will index your memberships.user_id key which seems ok. But your document size will grow as you will add viewers, editors and administrators. Also, your schema will make it difficult to make querys like:

Query projects, where user_id xxx is editor:

Again, you maybe do not need to query projects like this, so your schema looks fine. But if you need to query your projects by user_id AND role, i would recommend you creating a 'project_membership' collection :

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 easier... add an index on your project schema:

db.projects.ensureIndex({"memberships.role": 1})

Upvotes: 3

Related Questions