0

So I have this MEAN-project I hobby on in my spare time. Right now I'm setting up users and rooms, and am a bit hesitant about progressing further, as I am unsure about the proper protocol of db's in general.

As I recall, you're not supposed to have a Many-To-Many relationship; rather, you're supposed to have a relation table.

Right now, my User schema has an array of rooms he is in, and my Room schema has an array of users tied to it (the third and last schema being Message).

Is it better to have a userroomrelation doc that holds a PK, an id of one room, and then a list of all users in this room?

Thanks, Rasmus

2 Answers2

0

MongoDB isn't a relational database like *SQL databases (hence why MongoDB is called NoSQL), so using a relation table is fairly inefficient in Mongo. Holding an array of user _id's in the room collection is about as ideal as you could get, if you don't want repeat data.

Here are some more indepth answers on many-to-many in MongoDB.

Kevin Hoerr
  • 2,319
  • 1
  • 10
  • 21
  • Alright, thanks, Kevin. I've been working too much with SQL I guess. – Rasmus Edvardsen Feb 09 '18 at 21:49
  • 1
    No worries. I've been using it for several months at work, and I still turn around at times and say "Why can't I just normalize this?!" It seems less logical but it's definitely simpler and great for small projects. When I do changes by hand tiny errors don't seem as potent. – Kevin Hoerr Feb 09 '18 at 22:01
0

How can a User be in more than one room? Isn't that just a property on the User? And if you index that why would you also need to store it on the Room?

There is no one right way, it really depends on how many of each object you have and if it's a small number (as rooms and users implies) you may be better with a simpler and more robust (cannot store impossible values) approach like having a single property on a user RoomId. That's never going to be inconsistent and if you need to find the set of users in a given room it's a cheap query.

In MongoDB you CAN denormalize the data and store an array on each object containing part or all of the other object, but you can also create an effective join collection if you want to.

For example you could have a collection {UserId, RoomId, DateTimeEntered, DateTimeLeft} with appropriate indexes which allows you to quickly find all the users in a given room at a given time. Once you have the set of Ids you could go load them if you need them for display OR you could add the fields you need for display to this table {UserId, UserName, ...} BUT then you have the problem of maintaining that data if it ever changes OR keeping it intact if you need to know that when they entered the room that's what it was called.

There are also a TON of other questions on StackOverflow relating to how you should store related data, I suggest you go read those also.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • Hey Ian, A user is allowed to be in more than one room. From an app point of view, he is obviously only utilizing one, however he can be in several different ones, receiving new messages from these. --- Could you elaborate a bit on this denormalization? Or is that gone with the wind what with multiple rooms? Thanks for your answers. – Rasmus Edvardsen Feb 09 '18 at 21:47