This particular section is mainly involved in discussing the 15 best tips and tricks for the MongoDB developers. Hope the following tips will help you understand it and follow in your project.
Tip 1: Duplicate The Data For Speed, Reference Data For Integrity
Data which are particularly used by the multiple documents can either be referenced (normalized) or embedded (de-normalized). Again, the de-normalization is generally considered to be better than normalization and vice versa and so, each has their own trade-offs and so, you can easily choose to consider whatever will be considered to be best with your application.
Tip 2: Normalize In Case You Need To Future Proof Data
Your data is mainly future-proofed with the help of normalization. So, for different application, you would be able to use normalized data for many different applications which will query the data in many different ways in the future. This helps in the assumption that you have some data set which application after application will have to use for years and years.
Tip 3: Try To Fetch Data In A Single Query
For some of the application work, mainly throughout the section, the application unit is considered to be a general term. You can possibly think of a particular application unit as a request to the backend in case you have a mobile application or web. Some important examples include:
- This might be a user interaction for a desktop application.
- Also, this might be one graph loaded for an analytics system.
Tip 4: Embed Dependent Fields
You need to ask yourself at the time of considering whether to reference or embed a particular document. This applies in case you will be querying for the information in this field by itself or only in the framework of the larger document. So, it is very important to embed the information in that document in case one document cares about certain information.
Tip 5: Embed Point-In-Time Data
Embedding of the point-in-time data is considered to be very much important. In this regard, you do not actually want the information in such an order to change in case a product gets a new thumbnail or goes on sale. So, usually embedding is important for any sorts of information like this where it is important to snapshot the data at a particular time.
Tip 6: Avoid Embedding Fields That Have Unbound Growth
It is always important to avoid embedding of the fields that have unbound growth. Because the way in which data is stored by MongoDB, it is considered to be inefficient to be appending information to the end of an array.
Tip 7: Pre-Population Of Anything Which You Can
It is considered to be more efficient to populate your document in case you know that your document is going to need in certain fields in the future. This applies whenever you first insert it than to create the fields as you go.
Tip 8: Preallocation Of The Space Whenever Possible
Wherever possible, it is very important to consider the preallocation of space. It is generally an optimization for once which you know that your documents usually grow to a certain size. But, they mainly start out at a smaller size.
Tip 9: Storage Of The Embedded Information In Arrays For The Anonymous Access
A question which generally comes up is whether it is required to embed information in an array or a sub-document. Now, these sub-documents are mainly used when you will know exactly what you will query for.
Tip 10: Designing Of The Documents To Be Self-Sufficient
MongoDB is always supposed to be a dumb and big data store. So, it just retrieves and stores data with almost no processing.
Usually, there are certain operations which cannot be done with the help of $-operators. The complexities of the queries that you must do can also be used to make a document self-sufficient for most applications.
Tip 12: Computation Of Aggregations As You Go
It is important to compute aggregations over time with the help of $ inc whenever possible.
Tip 13: Writing Of The Code Which Helps To Handle Issues Related To Data Integrity
You will generally need to keep your data consistent in your application given the advantages to de-normalizing and also schema-less nature of MongoDB.
Tip 14: Usage Of The Correct Types
Usually, the storage of the data by using the correct types will make your life much easier.
Tip 15: Overriding _id When You Possess Your Own Simple And Unique Id
You need to go ahead and then use the default ObjectId for _ids in case your data does not possess a naturally occurring unique field.
So, it is cleared from the above section, that the above given are mainly considered to be the best tips and tricks for the MongoDB developer which are needed to be known.