One collection vs several collections
-
Hi all,
we developped for a company a webApplication using a MongoDB database; We created 10 collections : persons (80000) , formations, performances (using the id of a person : 80000/year)... A lot of relation between the collections.Since we work with NodeBB for an other company : I am wondering why nodeBB use a only one collection and if i should transform my models of data in only one collections as nodeBB.
Anybody transform its models of data -
Well, remember that NodeBB is firstly designed with redis in mind (a simple in-memory key/value store) so that might affect the document design decisions they made for their mongodb collection.
Best Practice is documented and you should follow whichever path best suits your business logic and query structure. This should walk you through it:
http://docs.mongodb.org/manual/core/data-models/ -
Now... that said, I'm wondering if it would be possible to use multiple collections in MongoDB (perhaps @baris can chime in, since I'm just brainstorming).
Sure,
db.delete()
would be difficult if we didn't know what collection to descend into, but what if we diddb.collection('users').delete('user:4234')
, making.collection()
a chainable method that sets the collection for that database call?In redis, that wouldn't make sense, but we could code in that same method in the Redis db library, that basically does nothing and returns db object again...
-
Thank you very much for this link. I am really wondering
- if embedded documents in my collection persons is more efficient than saving them in the same collection with a different key.
- if keeping one collection for subscribtions, one for events and one for persons is better than one unique collection.
I think about it and read further more.
-
If you are only going to use mongodb definitely have multiple collections with indices that make sense. For example your users collection can have indices on username whereas your posts collection can have indices on content and userid etc.
Well, remember that NodeBB is firstly designed with redis in mind (a simple in-memory key/value store) so that might affect the document design decisions they made for their mongodb collection.
This is why we have one collection in our mongodb implementation. To make db.delete db.rename easy to implement. From the db perspective there is no difference between a post and user object they are both indexed by the
_key
field so finding them is easy. For sorting and querying we use sorted sets, they are implemented in mongo using indices onvalue
andscore
.if embedded documents in my collection persons is more efficient than saving them in the same collection with a different key.
I wouldn't suggest embedding lots of documents, mongodb has a document size limit of 16mb so it won't let you insert more users after some point.