Unbounded many:many relationships

I’m new to NOSQL and Mongo and am aware of the unbounded arrays anti pattern and the 16MB size limit for documents. After doing some research, I noticed that objects with a many:many relationship need to reference each other. For example, for argument’s sake, let’s say there are users and tasks - a user can have many tasks, and a task can have many users. I would think to structure the database like this:

user {
	_id: 123,
	name: ‘bob’,
	tasks: [987, 1, 2, 3, …, N] // task reference _ids

task {
	_id: 987,
	name: ‘water the plants’,
	users: [123, 1, 2, 3, …, N] // user reference _ids

So each task can reference its users and each user can reference its tasks. However, for argument’s sake, let’s say that both the number of users and tasks are unbounded. This would mean that the users array in the a task object is unbounded and could exceed the 16MB limit, and the tasks array in a user object is also unbounded and could exceed the 16MB limit. So my question is how could this many:many relationship scale without exceeding the 16MB limit?

I feel like I could be missing something here. Thanks for any help

Hi @Nick_Smith ,

Yes you are correct that incase of an “unbounded” arrays using one document to form this relationship will fail and is not advisable.

For this scenario we have a pattern called the outlier pattern where large relationship is bucket into several documents each holding a portion of the array. The other documents called overflow document .

This works for both sides of the relationship.


1 Like

Perfect, that should do it - thank you

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.