1

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!!

2 Answers 2

1

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.

1
  • got it. about 1 it is that I am dealing with transactions and I don't want things like collide id to happen. Although I know it probably wouldn't. Also I thought when I separate the collection I can do map-reduce more efficiently on some scenarios but maybe I really shouldn't worry about this kind of things. But I agree what you said is better!! Thanks! Jan 2, 2015 at 2:27
1
  1. About _id generation, as long as you don't specify _id explicitly, Mongo would handle the id for you. It generates different nonce for different collection. So you should not need to worry about duplicated _id across different collection. Reference here.

  2. I agree with other answer, use same collection and put an extra field to differentiate regular user and admin

Your Answer

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

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