Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB structure: single collection vs multiple smaller collections

I have a general database structure question. In my scenario I happen to be using mongodb.

I'm creating an application where a user can upload a list of songs (title, artist, etc.) but am not sure if I should have one songList collection for all users or a separate songList.user# collection for each individual user. The users can only ever query songs associated to them so user A will NEVER know about user B's songs.

Code Examples:

Multiple collections per user

db.songList.userA.find() {"title": "Some song of user A", "artist": "Some artist of user A"}  db.songList.userB.find() {"title": "Some song of user B", "artist": "Some artist of user B"} 
  • Pros
    • Smaller collection size to query
  • Cons
    • Maintainability
      • 1,000 users means 1,000 collections

vs single collection with an owning 'user' field

db.songList.find({"user":"A"}) {"title": "Some song of user A", "artist": "Some artist of user A", "user": "A"} 
  • Pros
    • Flexibility to query across users if need ever arised
  • Cons
    • Performance

I'm trying to build a pro/con list but still on the fence. Given that each user's songs are going to be isolated from each other which approach is better? My main concern is maintenance and query performance.

Thanks in advance.

like image 865
Steven Avatar asked Dec 10 '12 03:12

Steven


People also ask

Can a MongoDB database have multiple collections?

2) A collection for each new event that comes along, w/ collection to keep track of all event names. No index on event name needs as each event is stored in a different collection. // multiple collections, create new as needed db.

How many collections is too many MongoDB?

In general, try to limit your replica set to 10,000 collections.

Should MongoDB collections be plural?

Collections: plural in lower case: images, resumes, Document fields: lowerCamelCase, e.g. memberFirstName, fileName, etc.

Can a database have multiple collections?

A database can contain multiple collections, but a collection cannot span multiple databases. Likewise, a collection can contain multiple documents, but a document cannot span multiple collections.


1 Answers

I would recommend NOT to make separate collection per user.

Read the documentation

By default MongoDB has a limit of approximately 24,000 namespaces per database. Each namespace is 628 bytes, the .ns file is 16MB by default.

Each collection counts as a namespace, as does each index. Thus if every collection had one index, we can create up to 12,000 collections. The --nssize parameter allows you to increase this limit (see below).

Be aware that there is a certain minimum overhead per collection -- a few KB. Further, any index will require at least 8KB of data space as the b-tree page size is 8KB. Certain operations can get slow if there are a lot of collections and the meta data gets paged out.

So you won't be able to gracefully handle it if your users exceed the namespace limit. Also it won't be high on performance with the growth of your userbase.

UPDATE

As @Henry Liu mentioned in the comments. For Mongodb 3.0 or above using WiredTiger storage engine, it will no longer be the limit.

docs.mongodb.org/manual/reference/limits/#namespaces

like image 157
Sushant Gupta Avatar answered Sep 28 '22 04:09

Sushant Gupta