Chapter02 Setting Up the Document Store with MongoDB
01 Summary
FastAPI+React全栈开发06 使用MongoDB设置文档存储
In this chapter, we are going to address some of the main features of MongoDB, building upon what was mentioned in the introductory chapter, and then we will dive into a practical introduction through several simple yet illustrative examples. After reviewing the process of installation on a local machine, using Windows or Ubuntu (which is probably the most popular Linux distribution today), and creating an online account on Atlas, we will be covering the basic commands of the MongoDB querying framework that enable us to start as quickly as possible. We will walk you through the essential commands (methods) that will enable you to insert, manage, query, update, and wrangle your data. The aim of this chapter is not to make you a MongoDB expert or even a proficient user, but just to help you see how easy it can be to set up a system, be it on your local machine or on the cloud, and perform the operations that might arise in a fast paced web development process.
在本章中,我们将讨论MongoDB的一些主要特性,建立在介绍性章节中提到的基础上,然后我们将通过几个简单但说明性的例子深入到实际的介绍中。在回顾了本地机器上的安装过程之后,使用Windows或Ubuntu(这可能是当今最流行的Linux发行版),并在Atlas上创建一个在线帐户,我们将介绍MongoDB查询框架的基本命令,使我们能够尽快启动。我们将引导您了解基本命令(方法),这些命令(方法)将使您能够插入、管理、查询、更新和整理数据。本章的目的不是让你成为MongoDB专家或精通用户,而是帮助你了解设置系统是多么容易,无论是在本地机器上还是在云上,并执行在快节奏的web开发过程中可能出现的操作。
In this chapter, we will cover the following topics:
- The structure of a MongoDB database
- Installing MongoDB and friends
- MongoDB querying and CRUD operations
- Aggregation framework
在本章中,我们将讨论以下主题:
- MongoDB的数据库结构
- 安装MongoDB和好友
- MongoDB查询和CRUD操作
- 聚合框架
By the end of this chapter, you will be able to set up a MongoDB database in a local or online environment, and you will know the basics of data modeling with the most popular NoSQL database. Topics such as querying (through MongoDB methods and aggregation) are best learned through playing around with data. In this chapter, we have provided a simple yet interesting real-life dataset that will be your starting point. Finally, this chapter should enable you to import your own data and apply the principles from the following pages, building upon them and coming up with your own queries, aggregations, and data insights.
在本章结束时,你将能够在本地或在线环境中设置MongoDB数据库,并且你将了解最流行的NoSQL数据库数据建模的基础知识。诸如查询(通过MongoDB方法和聚合)之类的主题最好通过摆弄数据来学习。在本章中,我们提供了一个简单而有趣的现实数据集,这将是你的起点。最后,本章应该能让你导入你自己的数据,并应用下面几页的原理,在它们的基础上构建你自己的查询、聚合和数据洞察。
Technical requirements
MongoDB's latest version (version 5) requires Windows 10 64-bit or Windows Server 2019 or later. When it comes to Linux, the last three versions of Ubuntu (Debian) are supported. Any decent PC or laptop with at least 8 GB of RAM and a CPU not more than 5 years old should be more than enough to get you started. However, for full stack development, which means having a couple of processes running simultaneously, compiling the frontend, maybe some CSS processor, having an editor (we will use VS Code), and a browser with a dozen tabs open, if possible, we would recommend 16 GB ofr RAM and a big screen (protect your eyes because unfortunately, they are not upgradeable!).
MongoDB的最新版本(版本5)需要Windows 10 64位或Windows Server 2019或更高版本。说到Linux,支持Ubuntu (Debian)的最后三个版本。任何像样的个人电脑或笔记本电脑,至少有8gb的内存和一个不超过5年的CPU,应该足以让你开始。然而,对于全栈开发,这意味着有几个进程同时运行,编译前端,也许一些CSS处理器,有一个编辑器(我们将使用VS Code),和一个浏览器,打开了十二个选项卡,如果可能的话,我们建议16gb的RAM和一个大屏幕(保护你的眼睛,因为不幸的是,他们是不可升级的!)。
Summary
Trying to condense and reduce the key information about an ecosystem as vast and as feature rich as MongoDB is not an easy task, and we admit that this chapter is heavily influenced by a personal view of what the key takeaways and potential traps are. We learned the basic building blocks that define MongoDB and its structure, and we have seen how to set up a local system as well as an online Atlas account.
试图精简和减少一个像MongoDB这样庞大和功能丰富的生态系统的关键信息并不是一件容易的事情,我们承认这一章很大程度上受到了个人观点的影响,即关键要点和潜在陷阱是什么。我们学习了定义MongoDB及其结构的基本构建块,并了解了如何设置本地系统以及在线Atlas帐户。
You are now able to begin experimenting, importing your own data (in CSV or JSON), and playing with it. You know the basics of creating, updating, and deleting documents and you have a few simple but powerful tools in your developer's toolbox, such as the find method with its peculiar, yet powerful, filter object syntax, and the aggregation pipelines framework, a strong analytic tool in its own right. You are now able to set up a MongoDB shop anytime, anywhere; start with a free Atlas instance and begin coding, without thinking too much about the infrastructure and with the peace of mind that if, or rather when, the time comes to scale up and accommodate millions of users, your database layer will be ready and won't let you down.
现在您可以开始试验,导入您自己的数据(CSV或JSON格式),并使用它。您了解了创建、更新和删除文档的基本知识,并且在开发人员工具箱中拥有一些简单但功能强大的工具,例如find方法及其独特但功能强大的过滤器对象语法,以及聚合管道框架,它本身就是一个强大的分析工具。您现在可以随时随地设置MongoDB商店;从一个免费的Atlas实例开始编写代码,而不必过多考虑基础设施,并且可以放心,如果(或者更确切地说,当)需要扩展并容纳数百万用户时,您的数据库层将准备就绪,不会让您失望。
In the next chapter, we are going to dive into the process of creating APIs, application programming interfaces with FastAPI, an exciting and new Python framework. We will try to provide a minimal, yet complete guide of the main concepts and features that should hopefully convince you that building APIs can be fast, efficient, and fun. Since learning how to build REST APIs with a framework is much easier through practice, we will create a very simple, yet comprehensive API that will allow us to put our freshly created database to good use. We will be able to create new (used) car entries, delete them, update them, and learn how FastAPI solves most typical development problems along the way.
在下一章中,我们将深入到创建api的过程中,使用FastAPI创建应用程序编程接口,FastAPI是一个令人兴奋的新Python框架。我们将尝试提供一个最小但完整的主要概念和功能指南,希望能让您相信构建api可以是快速、高效和有趣的。由于通过实践学习如何使用框架构建REST API要容易得多,因此我们将创建一个非常简单但全面的API,使我们能够很好地使用新创建的数据库。我们将能够创建新的(旧的)汽车条目,删除它们,更新它们,并学习FastAPI如何解决最典型的开发问题。