Mongodb – Separate login for user / administrators using mongodb

database-designdatabase-recommendationmongodb

I have 2 separate groups of users on my website. One is user and the other one is admin. Currently I put them in 2 different collections user and admin and login respectively. My questions is:

  1. Mongodb generates _id automatically for both user and the admin collection. Since they're in 2 different collections chances are one day you created one user and one admin with the same _id right? So it seems the only way is to check all documents in both dbs and make sure your newly created user (or admin) does not have repeated _id right? Is there something I can do natively in mongodb to make this happen without checking?

  2. I can also put all users and admins in one collection. This way I don't have a problem but I am not sure if I should do this.

p.s: because I have a table keeping track of user / admin balances so I certainly don't want the _id to repeat
p.s2: the db is taking longer and longer to query. Also my ORM creates user instances and generated _id automatically for me. I am not sure if I should just generate an _id and create it this way…

Any comments are greatly appreciated. Thanks!!

Best Answer

You should not have two collections, that will give you headache.

Have a user collection which has admin and regular users. You can then query users as a whole or look for a given type only. So if you are looking for user id x you don't have two search two collections.

As for the question on duplicate ids with 2 collections, it simply depends how the ids are generated. You say your ORM does it... so you will need to check the doc on how it does it.