What about MongoDB?

Let’s talk about mongodb

Good day! I recently talked about Django, today we’ll talk about mongodb. In this blog, you will learn what mongodb is, how to install it, how to make the first application and learn the basic operations.

Introduction to mongodb

When developing applications, programmers are confronted every day with the need for flexible information storage, i.e., updating, searching through it, etc. The class of products that solve this circle of problems is familiar to everyone — the Database. But what is it in our understanding? For many, the “database” is firmly associated with MySQL, tables, and SQL queries. And it suits up to a certain point. Indeed, relational databases provide a lot of advantages in work: since the data is strongly connected, there is no need to monitor the integrity of the database. Using a simple sub-request, you can select the number of comments for each blog post. Using JOIN it is easy to make complex linked samples and retrieve data on several entities at once.

However, when you suddenly realize that one server is no longer enough, and you want to scatter the base on several physical machines, the first thing offered is master-slave replication, in which the recording goes to one machine, and reading from several. But with a sufficiently large number of changes in the near future, master will only do what to distribute logs, and you will have to resort to sophisticated configuration so that each node has one single master server and can have several slave servers. This scheme is very difficult to implement and contains single point of failure (that is, when a single server fails, all of its subordinates are rapidly dying out). Again, all these tricks will increase only the number of readings. And if for making changes to the state of the database there is not enough power of one server, then you have to distribute the load by storing different tables on different servers. But then connectedness will be lost. Or you have to break the table into several parts, storing them in different places, according to a given law (for example, by ID), but this will take the JOIN charms to the grave. The further we try to scale relational databases, the more convenience we pay for it. For example, when using master-master, we will pay with auto increments.

Mongodb is a sharp object-document database. Ideologically, this is a kind of symbiosis between a familiar relational database and key-value storage, and in my opinion, very successful. On the one hand, it allows you to do very fast operations on an object, knowing its identifier, and on the other hand, it provides a powerful tool for complex interactions.

Supports select operations (count, group, MapReduce …), inserts, changes, and deletes. There are no connections between objects, objects can only store other objects in properties. Both unique and composite indexes are supported. Indices can be imposed on the properties of nested objects.

Replication is supported (even implied), fail-over is implemented.

Implemented MapReduce and sharding.

Since objects can have an arbitrary set of properties, it is enough for the catalog to create a collection of goods and add objects there. In this case, the search will be conducted by index.

The sharpness of MongoDB is pronounced on insert, they happen well very quickly. By the way, it’s nice that the storage format and the format of transferring objects over the network are the same, so to select an object you just need to find its position by index and return a piece of a file of a certain length to the client — no abstraction over the storage engine.

The unique identifier is not an auto-increment field, but a 12-byte unique number generated on the client. Thus, firstly, there is no problem with synchronization of replicas, i.e. You can independently insert into two different machines, and there will be no conflict. Secondly, there will be no nonsense with an overflow of an integer, well, and after the re-creation of the database, the search engines will not address the new articles on the old links.

I talked a little about mongodb and its benefits, now it’s time to install it.

Applications using mongodb
A lot of companies use mongodb for applications, thanks to the advantages that I wrote earlier. Specific examples of using the mongodb example: amazon.com, netflix.com, ebay.com the Chicago government.

Installing mongodb

To install mongodb you need to follow the link https://www.mongodb.com/download-center/community

Choose your operating system and click download msi file. After downloading the installer, open it. Next comes the standard installation: accept the license agreement, then click next. After that, you will be prompted to choose the type of installation, click complete. On the next page, leave the settings as standard and make sure that the Run service as Network Service user button is pressed. After that, you will be prompted to install mongodb compass, a graphical interface for mongodb. Make sure the Install mongodb compass button is pressed. Click next and the installation process will begin. After successful installation, you will see Completed mongoDB. Now mongoDB is on your operating system. Now you need to create a folder on the C drive called data. Inside the data folder, create a folder called db. If you have not changed the installation path, you will find the mongodb folder on the C drive in the Program Files folder. When you came into the mongoDB folder, go to the server folder there will be another folder, open it.

If you’re using Windows, just hold down Shift and right-click on the bin folder, then click open command window. Now type mongod. When the process stops, open the command window as the first time, without closing the window, where you typed “mongod”. In the second command window, where you did not type “mongod”, type “mongo”. You have now installed mongodb.

If you are using a different operating system, copy the entire path to the bin folder.

Now open the command window and type cd C:\Program Files\MongoDB\Server\4.0\bin. I for an example inserted the way to the bin folder in my computer, you can have another way to the bin folder. You are now logged into the bin folder. Then do the same manipulations as on Windows. Type mongod. When the process is stopped, open a command window as the first time, without closing the window, where you typed “mongod”. Remember to specify the path to the bin folder again. In the second command window, where you did not type “mongod”, type “mongo”. You have now installed mongodb.

Create your first own database.

Now it’s time to create a database, let it be a library.

To create a database, use the use command. Assume that the database is called mylib. In the command prompt window where you typed Mongo, type use mylib. You can insert any name instead of mylib. Although we have already created a database, if we refer to show databases using the show dbs command, we will not see the mylib database because we need to insert some data into the database.

To insert data, you need to enter this command, just make sure you typed use mylib and the command line output is switched to db mylib :

db.name_of_collection.insert ({“name”: “Django book”})

name_of_collection-the name of the collection

name-the name of the key

The Django book is the key name value

then press enter.

Now, if you order the show dbs command to display the databases, you will see the mylib database, which means you created the first database!!!!

Applications that use mongodb

Aadhar

The unique Indian project is the largest biometric database in the world. Aadhar is in the process of collecting biometric and demographic data from more than 1.2 billion people. Aadhar used mongodb as one of the databases to store such a large amount of data. In Aadhar MySQL is used to store demographic data and MongoDB for storing images. According to techcrunch.com. MongoDB has nothing to do with” sensitive “ data.

Shutterfly

Shutterfly is a popular online photo sharing company. Its database contains 6 billion photos and up to 10,000 transactions per second. Shutterfly moved from Oracle to mongodb. Mongodb has increased the productivity of each programmer, as well as the scalability and productivity of the company. Until now, Shutterfly is pleased with its decision to switch to MongoDB and that Kenny Gorman (Data Architect of Shutterfly) should say about it: “I am firmly convinced in choosing the right tool for the job, and MongoDB was pleasant but not without compromises.”

MetLife

MetLife is the world’s leading provider of insurance, annuities and employee programs. MetLife uses mongodb to run the app for more than 90 million users. The Bach data app currently stores about 24 terabytes of information.

4.ebay

eBay - американская интернет-корпорация. Имеет ряд проектов для поиска клиентов, хранения метаданных и управления облаками, работающими на mongodb.

Well, in the end, I will tell you a little about basic operations in mongodb.

show dbs-shows all databases

use name_of_database -switches в базу данных с именем name_of_database

db.name_of_collection.insert ({“name”: “Django book”}) adds a name with a value of Django book in the collection named name_of_collection to the selected database

db-shows the selected database

db.drop Database () — deletes the selected database

db.createCollection (name, parameters) — creates a collection in the selected database

db.collection.drop () — deletes the collection from the selected database. collection — the name of the collection. If the program successfully deletes the collection, it returns true, otherwise, it returns false.

At this point, I advise you to practice mongodb! Train and you will succeed!