FastAPI+React全栈开发03 为什么使用MongoDB

Chapter01 Web Development and the FARM Stack

03 Why use MongoDB

FastAPI+React全栈开发03 为什么使用MongoDB

In the following paragraphs, we will go through the main features of our selected database system - MongoDB - and give a high-level overview of the features that make it an excellent fit for our FARM stack. After a brief introduction of some specificities of the database, in the following chapter, we will go over the setup and create a working database environment that will enable us to showcase some basic methods.

在下面的段落中,我们将介绍所选数据库系统MongoDB的主要特性,并对使其非常适合FARM堆栈的特性进行高级概述。在简要介绍了数据库的一些特性之后,在下一章中,我们将检查设置并创建一个工作的数据库环境,使我们能够展示一些基本方法。

MongoDB is the database of choice in the FARM stack. It is fast, scalable, and document-oriented database that enables flexible schemas and, thus interative and rapid development. MongoDB is able to accommodate data structures of varing complexities, and its querying and aggregation methods make it an excellent choice for a flexible REST API framework such as FastAPI, coupled with an official Python driver. It has a high level of adoption and maturity and is one of the pillars of the NoSQL data storage movement that took the web development world by storm a decade ago.

MongoDB是FARM堆栈中首选的数据库。它是一种快速的、可伸缩的、面向文档的数据库,支持灵活的模式和交互式的、快速的开发。MongoDB能够适应各种复杂的数据结构,它的查询和聚合方法使它成为灵活的REST API框架(如FastAPI)的绝佳选择,再加上官方的Python驱动程序。它具有很高的采用率和成熟度,是十年前席卷web开发世界的NoSQL数据存储运动的支柱之一。

The main features that make MongoDB an ideal candidate for a flexible and fast-paced development environment, prototyping, and iterative development are listed as follows:

  • Easy and cheap: It is easy and fast to set up using an online cloud service that offers a generous free tier, while local installation is always on option.
  • Flexibility: The NoSQL nature of the database enables extremely flexible models and fast iterations and modifications on the fly.
  • Web-friendly format: The native data format BSON is practically a binary version of JSON, which, in turn, is the de facto data format of the modern web, so no complex parsing or transformations are necessary.
  • Complex nested structures: MongoDB documents allow other documents and arrays of documents to be embedded, which naturally translates into the data flow of a modern data web app (for example, we can embed all of the comments into the blog post they refer to). Denormalization is encouraged.
  • Simple intuitive syntax: The methods for performing basic CRUD operations (that is create, read, update, and delete), coupled with powerful aggregation frameworks and projections, allow us to achieve mostly all data reads relatively simply through the use of drivers, and the commands should be intuitive for anyone with a bit of SQL experience.
  • Built with scalability in mind: MongoDB is built from the ground up with several objectives - scalability, speed, and the ability to handle huge (huMONGOus) amounts of data.
  • Community and documentation: Lastly, MongoDB is backed by a mature company and strong community, and it offers various tools to facilitate the development and prototyping process. For instance, Compass is a desktop application that enables users to manage and admister databases. The framework of the serverless functions is constantly being updated and upgraded, and there are excellent drivers for virtually every programming language.

使MongoDB成为灵活、快节奏开发环境、原型和迭代开发的理想候选者的主要特性如下:

  • 简单和便宜:使用在线云服务进行设置既简单又快速,该服务提供了一个慷慨的免费层,而本地安装始终是一个选项。
  • 灵活性:数据库的NoSQL特性使得模型非常灵活,并且可以快速迭代和修改。
  • web友好格式:本机数据格式BSON实际上是JSON的二进制版本,而JSON反过来又是现代web的事实上的数据格式,因此不需要复杂的解析或转换。
  • 复杂的嵌套结构:MongoDB文档允许嵌入其他文档和文档数组,这自然会转化为现代数据web应用程序的数据流(例如,我们可以将所有评论嵌入到他们引用的博客文章中)。鼓励非正规化。
  • 简单直观的语法:执行基本CRUD操作(即创建、读取、更新和删除)的方法,加上强大的聚合框架和投影,允许我们通过使用驱动程序相对简单地实现大多数所有数据读取,并且对于任何具有一点SQL经验的人来说,这些命令都应该是直观的。
  • 构建时考虑到可扩展性:MongoDB是从头开始构建的,有几个目标-可扩展性,速度和处理大量数据的能力。
  • 社区和文档:最后,MongoDB由一个成熟的公司和强大的社区支持,它提供了各种工具来促进开发和原型过程。例如,Compass是一个桌面应用程序,它使用户能够管理和管理数据库。无服务器功能的框架不断更新和升级,并且几乎每种编程语言都有优秀的驱动程序。

I believe that in some cases, and this includes a lot of cases, MongoDB should be your first choice, especially when you are designing something that still has a very fluid or vague specification, and let's be honest, that happens a lot more than we would like to admit.

我相信在某些情况下,这包括很多情况下,MongoDB应该是你的第一选择,特别是当你设计的东西仍然有一个非常流动或模糊的规范,让我们说实话,这种情况比我们想承认的要多得多。

Of course, MongoDB is not a silver bullet, and some drawbacks are worth noticing upfront. On the one hand, the schemaless design and the ability to insert any type of data into your database might be a bit panic, inducing but translates to the need for stronger data integrity validation on the backend side. We will see how Pydantic, an excellent Python validation and type-enforcement library, can help us with that. The absence of complex joins, which are present in the SQL world, might be a dealbreaker for some types of applications. For analytics-intensive applications that require numerous complex queries, relational databases are a better, and often the only possible, solution. Finally, for mission critical applications that require adherence to the ACID principles (that is, atomicity, consistency, isolation, and durability) of transactions, MongoDB or any NoSQL database system migh not be the right solution.

当然,MongoDB不是灵丹妙药,一些缺点值得提前注意。一方面,无模式设计和将任何类型的数据插入数据库的能力可能会让人有点恐慌,这会导致后端需要更强的数据完整性验证。我们将看到Pydantic(一个优秀的Python验证和类型强制库)如何帮助我们实现这一点。缺少SQL世界中存在的复杂连接,可能会破坏某些类型的应用程序。对于需要大量复杂查询的分析密集型应用程序,关系数据库是更好的解决方案,而且通常是唯一可能的解决方案。最后,对于需要遵守事务的ACID原则(即原子性、一致性、隔离性和持久性)的关键任务应用程序,MongoDB或任何NoSQL数据库系统可能不是正确的解决方案。

Now that we understand what MongoDB brings to the table in terms of scalability, but especially flexibility with its schema less approach, let us take a look at the REST API framework of choice, FastAPI, and learn how it can help us leverage that schema-less approach and simplify our interactions with the data.

现在我们已经了解了MongoDB在可伸缩性方面带来了什么,尤其是它的无模式方法的灵活性,让我们来看看选择的REST API框架FastAPI,并了解它如何帮助我们利用无模式方法并简化我们与数据的交互。

相关推荐
Jacky(易小天)2 小时前
MongoDB比较查询操作符中英对照表及实例详解
数据库·mongodb·typescript·比较操作符
Loganer4 小时前
MongoDB分片集群搭建
数据库·mongodb
Rverdoser8 小时前
Linux环境开启MongoDB的安全认证
linux·安全·mongodb
inventecsh10 小时前
mongodb基础操作
数据库·mongodb
GISer_Jing12 小时前
React核心功能详解(一)
前端·react.js·前端框架
懒是一种态度14 小时前
Golang 调用 mongodb 的函数
数据库·mongodb·golang
FØund40415 小时前
antd form.setFieldsValue问题总结
前端·react.js·typescript·html
疯狂的沙粒16 小时前
如何在 React 项目中应用 TypeScript?应该注意那些点?结合实际项目示例及代码进行讲解!
react.js·typescript
鑫宝Code16 小时前
【React】React Router:深入理解前端路由的工作原理
前端·react.js·前端框架
沉默璇年1 天前
react中useMemo的使用场景
前端·react.js·前端框架