How to design this mapping (many:many) in MongoDB?

kheya

I have one collection: Users

[
  { _id: bob, name: Bob, email:..},
  { _id: rob, name: Rob, email:..},
  { _id: job, name: Job, email:..},
  ...
  { _id: zzz, name: ZzZ, email:...} 
]

I need a mapping table that shows who follows whom: I Have 2 arrays to hold followers and followings. Same problem, array may blow up.

user_followers collection like this:

    [
      {
        user: Bob,
        followers: [Mike, Steve, John, Mark, Raj],
        ..,
        following: [Alex, John, Mark]]
      }
    ]

How can I have a collection like this?

Gorkk

Your option #3 seems the best option to me (and about the only one considering the volume you're talking about), for the reasons you identified. Your option #1 was not really an option though, as you would have been in real trouble to query for all users Mike is following (you could only easily query who are the followers of a particular user).

Now for option #3 and your id issue, if I'm not mistaken (disclaimer: I've not used mongodb in about a year), you can use anything as an _id, including an object.

So for what you want you'd just use objects of type { user: 'bob', follower: 'Mike' } as _id (the requirement for _id is that is unique) and would thus have entries like

{
  _id: {
    user: 'bob',
    follower: 'Mike'
  }
}

Update

It seems I remembered correctly about the possibility to use an object for _id, as per the documentation:

Documents have the following restrictions on field names:

  • The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array.
  • The field names cannot start with the $ character.
  • The field names cannot contain the . character.

Update 2

Though with using a { user, follower } object as an id, you will probably need for efficient queries to add two indexes on _id.user and _id.follower (or at least a key on _id.follower, _id.user) to efficiently query for "all users following bob" and "all users that bob is following".

  • "all users following bob" can be queried efficiently with the _id index
  • "all users that bob is following" can't, and would most likely need a _id.follower index or a _id.follower, _id.user index
  • queries like "all users following either bob or jane" would a priori not be efficient with only the _id index.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related