Erik
Erik

Reputation: 14750

mongodb document structure

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?

Upvotes: 1

Views: 546

Answers (2)

ksol
ksol

Reputation: 12235

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.

Upvotes: 0

milan
milan

Reputation: 12412

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.

Upvotes: 2

Related Questions