How to organise a many to many relationship in MongoDB

后端 未结 3 2055
鱼传尺愫
鱼传尺愫 2020-12-01 01:22

I have two tables/collections; Users and Groups. A user can be a member of any number of groups and a user can also be an owner of any number of groups. In a relational data

3条回答
  •  隐瞒了意图╮
    2020-12-01 02:04

    Let's understand Many to Many Relations with an examples

    • books to authors
    • students to teachers

    The books to authors is a few to few relationship, so we can have either an array of books or authors inside another's document. Same goes for students to teachers. We could also embed at the risk of duplication. However this will required that each student has a teacher in the system before insertion and vice versa. The application logic may always not allow it. In other words, the parent object must exist for the child object to exist.

    But when you have many to many relationship, use two collections and have a true linking.

提交回复
热议问题