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

I register the various actions that users do on our website. Each action can be of a different type: comment, search query, page view, voting, etc. Each of these types has its own schema and general information. 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 develop an Action class / table and a set of inherited classes / tables (comment, search, vote).

Since MongoDb is smaller than a schema, I tend to set up a unique collection ("Actions"), where I would save these objects instead of several collections (a collection of Actions + collection Comments with a link key to its parent Action, etc ...).

My question is: what about runtime / response if I try to search on specific columns?

As I understand it, indexing best practices, if I want "every user to search mongodb", I would index the columns "type" + "query". But this will not concern the entire data set, only for the "search" type.

Will the MongoDb mechanism check the entire table or just focus on data having this particular schema?

+4
source share
2 answers

If you create sparse indexes , mongo will ignore any rows that do not have a key. Although there is a certain limitation on sparse indexes, they can only index one field .

However, if you are only going to request common fields, there is no reason not to use one collection.

those. if an index of type user + (or date + type of user +) will satisfy all your requests - there is no reason to create several collections.

Tip. Use date objects for dates, use identifiers of objects where names are not specified.

+3
source

Here is some useful information from MongoDB Best Recommendations

Store all data for recording in one document.

MongoDB provides atomic operations at the document level. When the data since the record is stored in one document, the entire record can be retrieved in one search operation, which is very effective. In some cases, it may not be practical to store all the data in one document, or it may adversely affect other operations. Make the tradeoffs that work best for your application.

Avoid large documents.

The maximum size of documents in MongoDB is 16 MB. In practice, most documents are a few kilobytes or less. Consider documents that look like rows in a table than tables themselves. Instead of maintaining lists of records in one document, instead make each record a document. For large multimedia documents such as video, GridFS, an agreement implemented by all drivers that store binary data in many small documents.

0
source

Source: https://habr.com/ru/post/1403037/


All Articles