Home > OS >  NoSql data storage pattern
NoSql data storage pattern

Time:12-06

If I've got the following classes for a hypothetical football app:

Manager, Player & Team

A Manager has many Players.

A Manager can put together a Team for a game, and that will contain a number of Players

Players can be associated to a Manager, a Team and to the general transfer market simultaneously.

So when I'm thinking about storing data in the Team about Players, would this just be an array of Player's Ids? I guess the same question about the Manager, the Manager would own Players, but would they be an array of Player Ids?

Thanks

CodePudding user response:

Yes, you could store the Players that are associated with a Manager or a Team as an array of Player IDs in a NoSQL database. This would allow you to reference the Players without having to duplicate their data.

In the case of a Manager, you could have a players property on the Manager document that is an array of Player IDs. This would represent the Players that are associated with that Manager.

For a Team, you could have a similar players property that is an array of Player IDs, representing the Players that are on that Team.

When a Player is associated with a Manager or a Team, you would simply add their ID to the appropriate array. And when a Player is transferred from one Manager or Team to another, you would update the document to remove their ID from the old array and add it to the new one.

However, it is important to note that this approach does not enforce any relationships between the objects. For example, if you add a Player ID to a Manager's players array, there is no guarantee that the Player with that ID actually exists or is associated with that Manager. You would need to implement additional logic to ensure the integrity of the data, such as by using a database that supports referential integrity or by implementing your own validation checks.

  • Related