Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb : multiple specific collections or one "store-it-all" collection for performance / indexing

Tags:

mongodb

nosql

I'm logging different actions users make on our website. Each action can be of different type : a comment, a search query, a page view, a vote etc... Each of these types has its own schema and common infos. For instance :

comment : {"_id":(mongoId), "type":"comment", "date":4/7/2012, 
           "user":"Franck", "text":"This is a sample comment"}

search : {"_id":(mongoId), "type":"search", "date":4/6/2012, 
          "user":"Franck", "query":"mongodb"} etc...

Basically, in OOP or RDBMS, I would design an Action class / table and a set of inherited classes / tables (Comment, Search, Vote).

As MongoDb is schema less, I'm inclined to set up a unique collection ("Actions") where I would store these objects instead of multiple collections (collection Actions + collection Comments with a link key to its parent Action etc...).

My question is : what about performance / response time if I try to search by specific columns ?

As I understand indexing best practices, if I want "every users searching for mongodb", I would index columns "type" + "query". But it will not concern the whole set of data, only those of type "search".

Will MongoDb engine scan the whole table or merely focus on data having this specific schema ?

like image 483
theAndroid Avatar asked Mar 23 '12 09:03

theAndroid


People also ask

Can a MongoDB have multiple collections?

Yes, you can have multiple collections within a database in MongoDB. Collections can be thought of as analogous to tables in relational databases.

How many indexes does MongoDB create by default for a new collection?

MongoDB provides two geospatial indexes known as 2d indexes and 2d sphere indexes using these indexes we can query geospatial data.

How many collections is too many MongoDB?

In general, we recommend limiting collections to 10,000 per replica set. When users begin exceeding 10,000 collections, they typically see decreases in performance. To avoid this anti-pattern, examine your database and remove unnecessary collections.

Can MongoDB handle millions of records?

Working with MongoDB and ElasticSearch is an accurate decision to process millions of records in real-time. These structures and concepts could be applied to larger datasets and will work extremely well too.


1 Answers

If you create sparse indexes mongo will ignore any rows that don't have the key. Though there is the specific limitation of sparse indexes that they can only index one field.

However, if you are only going to query using common fields there's absolutely no reason not to use a single collection.

I.e. if an index on user+type (or date+user+type) will satisfy all your querying needs - there's no reason to create multiple collections

Tip: use date objects for dates, use object ids not names where appropriate.

like image 199
AD7six Avatar answered Nov 15 '22 05:11

AD7six