DBWeinstein
DBWeinstein

Reputation: 9479

In MongoDB, should users and usage data be in separate collections?

I'm going to be tracking usage data (clicks, emails...etc) for users of my product. I'm using MongoDB and I'm wondering if I even need a separate collection for the usage data. Why not just keep added the data to the user documents using embedded documents?

Upvotes: 1

Views: 70

Answers (1)

mnemosyn
mnemosyn

Reputation: 46301

Why not just keep added the data to the user documents using embedded documents?

  1. This will make writes to a very sensitive collection from parts of the code that are relatively insecure, thereby coupling parts of the code that shouldn't be coupled
  2. It will trigger a high number of writes on an otherwise mostly-read collection, severely increasing fragmentation
  3. This kind of data (clicks, emails, page views, etc.) has a very wide distribution, i.e. some users are mostly inactive, others are highly active, thus the size of documents will be largely spread and could exceed the maximum document size 3a. by the same token, attackers could flood your user collection
  4. additionally, this slows down fetching users because there's more data to fetch. Even when using a projection, the database will still have to read the data from disk or RAM

In a nutshell: you can do it for small systems with low nonfunctional requirements, but I don't recommend it for anything serious.

Upvotes: 1

Related Questions