Monday, March 1, 2021

Python and MongoDB: Connecting to NoSQL Databases

MongoDB is a document-oriented and NoSQL database solution that provides great scalability and flexibility along with a powerful querying system. With MongoDB and Python, you can develop many different types of database applications quickly. So if your Python application needs a database that’s just as flexible as the language itself, then MongoDB is for you.

In this tutorial, you’ll learn:

  • What MongoDB is
  • How to install and run MongoDB
  • How to work with MongoDB databases
  • How to use the low-level PyMongo driver to interface with MongoDB
  • How to use the high-level MongoEngine object-document mapper (ODM)

Throughout this tutorial, you’ll write a couple of examples that will demonstrate the flexibility and power of MongoDB and its great Python support. To download the source code for those examples, click the link below:

Using SQL vs NoSQL Databases

For decades, SQL databases were one of the only choices for developers looking to build large and scalable database systems. However, the increasing need for storing complex data structures led to the birth of NoSQL databases. This new kind of database system allows developers to store heterogeneous and structureless data efficiently.

In general, NoSQL database systems store and retrieve data in a much different way from SQL relational database management systems (RDBMSs).

When it comes to choosing from the currently available database technologies, you might need to decide between using SQL or NoSQL systems. Both of them have specific features that you should consider when choosing one or the other. Here are some of their more substantial differences:

Property SQL Databases NoSQL Databases
Data model Relational Nonrelational
Structure Table-based, with columns and rows Document based, key-value pairs, graph, or wide-column
Schema A predefined and strict schema in which every record (row) is of the same nature and possesses the same properties A dynamic schema or schemaless which means that records don’t need to be of the same nature
Query language Structured Query Language (SQL) Varies from database to database
Scalability Vertical Horizontal
ACID transactions Supported Supported, depending on the specific NoSQL database
Ability to add new properties Need to alter the schema first Possible without disturbing anything

There are many other differences between the two types of databases, but those mentioned above are some of the more important ones to know about.

When choosing a database, you should consider its strengths and weaknesses carefully. You also need to consider how the database fits into your specific scenario and your application’s requirements. Sometimes the right solution is to use a combination of SQL and NoSQL databases to handle different aspects of a broader system.

Some common examples of SQL databases include:

NoSQL database examples include:

In recent years, SQL and NoSQL databases have even begun to merge. For example, database systems, such as PostgreSQL, MySQL, and Microsoft SQL Server now support storing and querying JSON data, much like NoSQL databases. With this, you can now achieve many of the same results with both technologies. But you still don’t get many of the NoSQL features, such as horizontal scaling and the user-friendly interface.

With this brief background on SQL and NoSQL databases, you can focus on the main topic of this tutorial: the MongoDB database and how to use it in Python.

Managing NoSQL Databases With MongoDB

MongoDB is a document-oriented database classified as NoSQL. It’s become popular throughout the industry in recent years and integrates extremely well with Python. Unlike traditional SQL RDBMSs, MongoDB uses collections of documents instead of tables of rows to organize and store data.

MongoDB stores data in schemaless and flexible JSON-like documents. Here, schemaless means that you can have documents with a different set of fields in the same collection, without the need for satisfying a rigid table schema.

You can change the structure of your documents and data over time, which results in a flexible system that allows you to quickly adapt to requirement changes without the need for a complex process of data migration. However, the trade-off in changing the structure of new documents is that exiting documents become inconsistent with the updated schema. So this is a topic that needs to be managed with care.

MongoDB is written in C++ and actively developed by MongoDB Inc. It runs on all major platforms, such as macOS, Windows, Solaris, and most Linux distributions. In general, there are three main development goals behind the MongoDB database:

  1. Scale well
  2. Store rich data structures
  3. Provide a sophisticated query mechanism

Read the full article at https://realpython.com/introduction-to-mongodb-and-python/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...