Take the 2-minute tour ×

My database has users collection, each user has multiple documents, each document has multiple sections each section has multiple works

Users work with works collection very often (add new work, update works, delete works). So my question is what structure of collections should I make? works collection is 100-200 records per section.

Should I make work collection for all users with user _id or there is best solution?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Depends on what kind of queries you have. The guideline is to arrange documents so that you can fetch all you need in ideally one query.

On the other hand, what you probably want to avoid is to have mongo reallocate documents because there's not enough space for a in-place update. You can do that by preallocating enough space, or extracting that frequently changing part into its own collection.

share|improve this answer
you can achieve that with few different document structures, but some of them will fit better your use case. that's what i was trying to say. –  milan Jan 14 '12 at 10:10
it's difficult since i don't have enough details. you can start like this, and evolve as you discover that something doesn't work well. for a start, i'd probably extract section into a separate collection. –  milan Jan 14 '12 at 10:22
when you fetch user account details, do you normally always want to see all his sections and works? just to get through the login process? probably not. that's one reason. of course, you'd just pull some fields not all sections. but it also makes sense domain-wise. you even talk about sections as being "documents" that users work on. –  milan Jan 14 '12 at 10:38

As you can read in MongoDB docs,

Generally, for "contains" relationships between entities, embedding should be be chosen. Use linking when not using linking would result in duplication of data.

So if each user has only access to his documents, I think you're good. Just keep in mind there's a limitation on size (16MB I think) for documents which you should be careful about, since you're embedding lots of stuff.

share|improve this answer
I don't think embedding documents in user is a good idea if that's what you're suggesting, even if a user only has access to their documents. Apart from anything else they're conceptually separate entities. –  Russell Jan 14 '12 at 13:43

Your Answer


