Introduction to MongoDB
What is MongoDB?
MongoDB is an open source, document-oriented, NoSQL database system. One of its main features is that it allows us to save our structures or documents in BSON format (a specification similar to JSON). These types of databases arise from a great demand for databases that can work with massive data more efficiently.
MongoDB is Schema Less which allows documents to have different structures without affecting their operation, something we cannot do with relational database tables.
SQL vs NoSQL Terminology
In MongoDB, the equivalent of relational database tables will be collections. Collections group documents, which would correspond to rows in the relational model.
– The databases: They are the physical containers for the collections. Each database has its own file in the file system of our computer or server.
– Collections: They are groups of documents. They are equivalent to tables in relational databases (only that collections can store documents with many different formats, instead of being subject to a fixed schema). Tables store records (rows), while collections store documents.
– Documents: They are records within the collections. These documents are made up of key-value pairs and are stored in BSON format which is a binary representation of JSON-based maps.
Type of data
– Strings: They are used to store texts.
– Numbers: Numeric values made up of digits (with or without a decimal point), for example, Doubles, Integers, 64-bit Integers, and Decimals.
– Boolean: True or false information (true or false).
– Date: They are used to save dates.
– Null: refers to a null or empty value.
– Undefined: indicates a data that has not been defined.
– Arrays: Arrays or lists of any other type of data, including other lists.
Advantages of NoSQL
Compared to relational databases, NoSQL databases are more scalable and offer higher performance; In addition, his data model addresses several issues that the relational model misses:
– Large volumes of structured, semi-structured and unstructured data that are constantly changing.
– Agile development sprints, rapid iteration of schematics and frequent code generation.
– Flexible and easy to use object-oriented programming.
– Geographically distributed scale-out architecture, rather than a monolithic architecture.
Clients for MongoDB
In addition to the server, we must have clients to interact with the database. We can use any of the following options:
– Mongo Shell, MongoDB interactive shell. With the installation of MongoDB and its tools, we can access its interactive console and perform our first interactions with MongoDB.
– MongoDBCompass, based on a graphical interface. It is an interactive tool to query, optimize and analyze your data in MongoDB.
– Studio3T Free, friendlier than the console, with a text editor where you can write a sequence of instructions and launch them against the server.
First steps in the console
The first thing we must do is open the terminal and launch the following command:
Now we are ready to execute certain operations against the database.
List all the databases we have.
Create a new database or switch to an existing database.
We use the `use` command, followed by the name of the database we want to put in it.
Verify the database currently in use, that is, to know in which database I am positioned.
Delete a database.
1. First we list all the databases.
2. We use the `use` command to switch to the database we want.
3. We verify with the `db` command that we are in the correct database.
4. If it’s the one you want to drop and you’re sure, type the `db.dropDatabase()` command.
Although it is often said that NoSQL databases are narrow in scope, MongoDB can be used in many of the projects we develop today.
Any application that needs to store semi-structured data can use MongoDB. This is the case of typical CRUD applications or many of the current web developments. In my case, I have used MongoDB in personal projects such as creating online stores, creating applications, in systems with large amounts of documents and especially in those that need scalability, since MongoDB is especially useful in environments that require scalability and You don’t have so many limitations when it comes to climbing. Thanks to its simple replication options, we will obtain a system that scales horizontally and without too many problems, and we will even benefit from greater readability and speed.
MongoDB gives us the flexibility to decide the best design according to our convenience, and it will allow us to implement more agile work systems. Of course, it is important to take into account that although MongoDB collections do not require a defined schema, we should try to create one so that we can follow it ourselves. In this way, we will improve the performance of our application.
Over the years, MongoDB has become a trusted solution for many companies looking for a powerful and highly scalable NoSQL database. But MongoDB is much more than a traditional document-based database and has some great capabilities that set it apart from other DBMSs, for example, as enterprise applications scale and resource demands increase, problems can arise. to ensure the availability and reliability of services. MongoDB’s load balancing and sharing process distributes large data sets across multiple virtual machines simultaneously, while still maintaining acceptable read and write performance.
One of MongoDB’s biggest advantages over other databases is its ability to handle ad hoc queries that don’t require predefined schemas. MongoDB databases use a query language similar to SQL databases, making it extremely accessible for both beginning and advanced developers. This accessibility facilitates the insertion, consultation, classification, update and Export your data with common helper methods and simple shell commands.