I have a users index which has fields like id, email, password, etc. A user can see all other users except the ones who had blocked him. I want to design a database schema for this problem.

So I'm using MongoDB and I have these two tables.


id   email   password
01   [email protected]  123
02   [email protected]  XYZ

now, I need a way to store the block relations information. If x has blocked y then y can't see x on the user's list.

If I add an array in user y which contains all user id who has blocked them so would it be a good solution? or is there any other alternative?


id   email   password   blockedBy
01   [email protected]  123        []
02   [email protected]  XYZ        [01, 07]

now, I'll show all the users to user 02 except 01 and 07.

2 Answers 2


While this would work, IMHO, this isn't ideal because of the odd permissions / security setup that would be required. In your setup, user 1 would require access to user 2's record. Similarly, user 2 could potentially change their list of blocking users.

You could of course, setup a security scheme that would work, but, to me, it just seems awkward and not intuitive, which means it will become brittle and prone to breaking in the future as your schema evolves.

Generally speaking, it's much easier conceptually to assume that a user has permission to alter his own record, and no one else's (set aside group permissions for now). Creating a security scheme that safely grants access for one user to alter another user's record can become tricky and prone to errors.

I think it would be simpler to create a separate collection to hold the blocked relation. Something as simple as this could work: {blocker: 01, blockee: 02}. If you put an index, you won't lose performance over storing everything in a single collection.


If the information fits inside the record, go for it.

But remember that there is a limit to it, if you limit the block to something like 1000 users, should be fine.

When you are not in the position to define limits, then you should create another collection to handle this relation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.