shrewquest
shrewquest

Reputation: 551

When to create a new database vs a new collection vs a new cluster in mongodb?

When would I create a new database in MongoDB - does creating a separate database offer any advantage? From my understanding all the databases in a cluster share the same hardware resources so there’s no advantage there. Same for collections.

When would I create a new collection (table in RDBMS)? Only when the data looks different from other collections? or is there some sort of configuration, isolation etc that creating a new collection vs using the same collection provides?

Upvotes: 2

Views: 872

Answers (1)

D. SM
D. SM

Reputation: 14490

Databases are created for, among other reasons:

  • Separating authorization (scoping users to databases)
  • Localizing impact of actions (e.g. dropping all collections)
  • Making backups easier (dump entire database vs specifying a list of collections)

Collections are created for, among other reasons:

  • Limiting how much data queries have to traverse to give answers (collection scans & index scans)
  • Scoping indexes to portions of the data (to lower disk consumption by indexes)
  • Localizing impact of actions (e.g. deleting all documents in a collection)

It is possible to put all data into a single collection in a single database. https://www.thedailywtf.com/ has examples of (anonymized) applications doing that. See e.g. https://thedailywtf.com/articles/dynamic-tables. Usually the result is unmaintainable.

Upvotes: 2

Related Questions