MongoDB Scenarios – Best Scenarios For Using MongoDB

MongoDB Tutorials

MongoDB Scenarios – Best Scenarios For Using MongoDB from Coding compilerNow you should have the feeling that when and where to integrate MongoDB into your existing system is the best. There are a lot of new similar storage technologies that will definitely make you confuse when you choose. Let’s start discussing on MongoDB scenarios here now.

MongoDB Scenarios

Best MongoDB Scenarios – The most important lesson is not related to MongoDB, which means you don’t have to rely on a single solution to process your data. There is no doubt that a single solution has obvious advantages, and for many projects – or most – a single solution is a wise choice. It doesn’t mean you have to use different technologies, but rather you can. Only you know that the benefits of introducing new technologies outweigh the disadvantages.

Related MongoDB Tutorials

What is MongoDB Tutorial For Beginners
MongoDB Basics Tutorial
MongoDB Update Tutorial
MongoDB Query Tutorial
MongoDB Data Modelling Tutorial

Having said that, I hope that you have learned so far that you feel that MongoDB is a universal solution. We have mentioned many times that document-oriented databases and relational databases have many similarities. 

So instead of circumventing these same points, we can simply think that MongoDB is an alternative to relational databases. For example, using Lucene as a full-text search index for relational databases, or using Redis as a persistent key-value store, MongoDB is used to store your data.

Note that I did not say to replace the relational database with MongoDB, but instead of the case. There are many tools that it can do. Some things MongoDB can do better, and some MongoDB do almost. Let’s discuss it further.

MongoDB Flexible Schema

One of the benefits of a document-oriented database is that it does not require a fixed pattern. This makes them much more flexible than traditional database tables. I agree that modelessness is a very nice feature, but not what most people say.

When people talk about modelessness, it seems that you will save a bunch of messy data. It’s true that some areas of data are painful to model with relational databases, but I think these are unusual exceptions. 

Modeless is cool, but in most cases, your data structure should be well designed. It’s good to really deal with chaos, such as when you add a new feature, but the truth is, in most cases, an empty column can basically solve the problem.

For me, the real benefit of a dynamic mode is that it doesn’t require a lot of setup and can reduce the resistance used in OOP. This is especially true when you use static languages. I used MongoDB in C# and Ruby, the difference is very obvious. 

Ruby’s dynamic nature and its popular ActiveRecord implementation have dramatically reduced the resistance to differences between object-oriented/relational development. This is not to say that MongoDB and Ruby don’t match, but that they are too good. Really, I think that MongoDB in the eyes of many Ruby developers is only a little improvement, and in the eyes of C# or Java developers, MongoDB brings about a drastic change in the way data interacts.

Suppose this problem is viewed from the perspective of the driver developer. Do you want to save an object? Serialize it to JSON (strictly BSON, but pretty much) and pass it to MongoDB. No need to do any attribute mapping or type mapping. The benefits of this simplicity are passed on to you, the terminal developer.

MongoDB Write Operation

A special role that MongoDB can do is in the log field. There are two things that make MongoDB writes very fast. First, you can choose to return immediately after sending a write operation command without waiting for the operation to complete. 

Second, you can control the write behaviour of data persistence. These settings, plus, can define a successful commit, which is successful after you have successfully obtained your data on many servers, and each write operation is configurable, which gives you a high degree of authority. Control write performance and data persistence.

In addition to these performance factors, log data is such a data set, and it is more advantageous to use a modeless set. Finally, MongoDB also provides a  capped collection . So far, all the collections we created by default are normal collections. We can use  db.createCollection commands to create a restricted collection and mark its restrictions:

//limit our capped collection to 1 megabyte
db.createCollection('logs', {capped: true,
    size: 1048576})

Old documents are automatically cleared when our restricted collection reaches the 1MB limit. Another limitation can be based on the number of documents, not the size, with  maxtags. 

Constrained collections have some very interesting properties. For example, you can update the document but you can’t change its size. The insertion order is set, so there is no need to provide an additional index to get time-based sorting. You can “tail” a restricted collection, just like you would in Unix  tail -f to process the file, get the latest data, If there is data, you don’t need to re-query it.

If you want your data to “expire”, based on time rather than the size of the entire collection, you can use  TTL index  , the so-called TTL is the abbreviation of “time-to-live”.

MongoDB Durability

Prior to 1.8, MongoDB did not support single server persistence. That is, if a server crashes, it may result in loss or corruption of data. The solution is to run a MongoDB copy on multiple servers (MongoDB supports replication). Journaling is a very important feature added to version 1.8. Starting with version 2.0 of MongoDB, logs are launched by default, which allows for quick recovery of the server, such as a server crash or power outage.

Persistence is just mentioned here, because there has been a lot of results around MongoDB’s lack of single server persistence in the past. This topic may continue to appear in future Google searches. But the information about the shortcomings of the lack of logging is outdated.

MongoDB Full-Text Search

True full-text search was recently added to MongoDB. It supports fifteen languages and supports stemming and stop words. In addition to the native MongoDB full-text search support, if you need a more powerful and comprehensive full-text search engine, you need to find another solution.

MongoDB Transactions

MongoDB does not support transactions. There are two alternatives, one that works well but has limitations, and the other is more cumbersome but flexible.

The first option is a variety of atomic update operations. As long as it can solve your problem, it is quite good. We have seen a few simple ones, such as  $inc and $set. There are also findAndModify commands like  that that can automatically update the document after it has been updated or deleted.

The second option, when the atomic operation is not satisfied, returns to the two-part commit. For a transaction, a two-part commit is like dereferencing a link manually. This is a storage-independent solution. 

Two-part commits are actually very common in the relational database world and are used to implement transactions between multiple databases. An example of the MongoDB wwebsite demonstratesthe most typical occasion (fund transfer). 

The general idea is to save the state of the transaction to the actual atomic update document and then manually perform the init-pending-commit/rollback process.

MongoDB supports inline documentation and its flexible schema design, making the two-step commit less painful, but it’s still not a good deal, especially when you’re new to it.

MongoDB Data Processing

Prior to version 2.2, MongoDB relied on MapReduce to solve most of the data processing. In version 2.2, it adds a powerful feature called aggregation framework or pipeline , so you only need to use complex methods for those that do not yet support the pipeline.

Unusual aggregations use MapReduce. In the next chapter, we will look at the details of the aggregation pipeline and MapReduce. Now, you can think of them as powerful and implemented in different ways  group by (for example). For very large data processing, you may want to use other tools, such as Hadoop. Fortunately, the two systems are complementary, there is a  MongoDB connector for Hadoop .

Of course, relational databases are also not good at parallel data processing. MongoDB has plans to improve the ability to process large data sets in future releases.

MongoDB Geospatial Query

A very powerful feature is that MongoDB supports  geospatial indexes . This allows you to save geoJSON or x and y coordinates to a document, query the document, use it  $near to get a set of coordinates, or  $within get a point in a rectangle or circle. 

MongoDB Tools and Maturity

You should already know the answer to this question. MongoDB is indeed much younger than most relational data. This question is really something you should consider, but how important it is, depending on what you do and how. 

Anyway, a good assessment can’t ignore the fact that MongoDB is young, and the tools available are not very good (although mature relational database tools are somewhat scum!). For example, its lack of support for decimal floating point numbers is clearly a problem (although not fatal) in systems that handle currency.

On the positive side, it provides the driver for most languages, the protocol is modern and simple, and the development speed is quite fast. MongoDB has been used in production environments by many companies. Although there are concerns, after verification, the fear will soon become a thing of the past.

Summary

In most cases, MongoDB can replace relational databases. It’s simpler and more straightforward; it’s faster and usually less restrictive to application developers. However, the lack of transaction support may be worth considering carefully. 

Related MongoDB Tutorials For Beginners

What is MongoDB Tutorial For Beginners

MongoDB Basics Tutorial

MongoDB Update Tutorial

MongoDB Query Tutorial

MongoDB Data Modelling Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *