Chapter02 Setting Up the Document Store with MongoDB
08 Installing MongoDB and friends
FastAPI+React全栈开发08 安装MongoDB
The MongoDB ecosystem is composed of different pieces of software, and I remember that when I was starting to play with it, there was some confusion. It is, in fact, quite straightforward as we will see. Let’s examine the following various components that we will be installing or using in the following part:
- MongoDB community Edition: a free full-fledged version of MongoDB that runs on all major operating systems (Linux, Windows, or macOS) and it is what we are going to use to play around with data locally.
- MongoDB Compass: a graphical user interface (GUI) for managing, querying, aggregating, and analyzing MongoDB data in a visual environment. Compass is a mature and useful tool that we’ll be using throughout our initial querying and aggregation explorations.
- MongoDB Atlas: the database-as-a-service solution from MongoDB. To be honest, this is one of the main resons MongoDB is a huge part of the FARM stack. It is relatively easy to set up and it relieves us from manually administering the database.
- MongoDB Shell: a command line shell that we can use not only to perform simple Create, Read, Update, Delete (CRUD) operations on our database, but also to perform administrative tasks such as creating and deleting databases, starting and stopping services, and similar job.
- MongoDB Database Tools: serveral command line utilities that enable administrators and developers to export or import data to and from a database, provide diagnostics, or enable manipulation of files stored in MongoDB’s GridFS system.
MongoDB的生态系统是由不同的软件组成的,我记得当我开始使用它的时候,有一些困惑。事实上,我们会看到,这很简单。让我们检查一下我们将在以下部分中安装或使用的以下各种组件:
- MongoDB社区版:一个免费的完整版本的MongoDB,运行在所有主要的操作系统(Linux, Windows,或macOS)上,这是我们要用它来玩本地数据。
- MongoDB Compass:一个图形用户界面(GUI),用于在可视化环境中管理、查询、聚合和分析MongoDB数据。Compass是一个成熟而有用的工具,我们将在最初的查询和聚合探索中使用它。
- MongoDB Atlas: MongoDB的数据库即服务解决方案。说实话,这是MongoDB成为FARM堆栈重要组成部分的主要原因之一。它的设置相对容易,并且使我们不必手动管理数据库。
- MongoDB Shell:一个命令行Shell,我们不仅可以使用它来执行简单的创建、读取、更新、删除(CRUD)操作,还可以执行管理任务,如创建和删除数据库,启动和停止服务,以及类似的工作。
- MongoDB数据库工具:几个命令行实用程序,使管理员和开发人员能够从数据库导出或导入数据,提供诊断,或允许操作存储在MongoDB的GridFS系统中的文件。
The MongoDB ecosystem is constantly evolving, and it is quite possible that when you read these pages, the latest version numbers will be higher or some utility might have changed its name. MongoDB recently released a product called Realm, which is a real-time development platform useful for building mobile apps or Internet of Thins(IoT) applications, for instance. We will not cover all of the steps necessary to install all the required software as we do not find a huge stack of screenshots particularly inspiring. We will instead focus on the overall procedure and try to pinpoint they key steps that are necessary in order to have a fully functional installation.
MongoDB的生态系统是不断发展的,很有可能当你阅读这些页面时,最新的版本号会更高,或者一些实用程序可能已经更改了它的名称。MongoDB最近发布了一个名为Realm的产品,这是一个实时开发平台,可用于构建移动应用程序或物联网(IoT)应用程序。我们不会涵盖安装所有所需软件所需的所有步骤,因为我们不会发现大量的截图特别鼓舞人心。相反,我们将重点放在整个过程上,并试图找出必要的关键步骤,以便有一个完整的功能安装。
Installing MongoDB and Compass on Docker
查询6.x的版本:
https://hub.docker.com/_/mongo/tags?page=1&name=6.
这里,我选择的版本是:6.0.14-jammy
拉取镜像:
docker pull mongo:6.0.14-jammy
创建容器:
docker run --name mongo -d --restart=always -p 27017:27017 mongo:6.0.14-jammy
之后使用python进行连接测试:
import time
from mongo6.pymongo import MongoClient
# 会开进程,主进程需要等待
client = MongoClient('mongodb://zhangdapeng:zhangdapeng520@192.168.77.129:27017/')
print(client)
# 等待1秒钟
time.sleep(1)
print("建立连接成功:", client)
安装MongoDB Compass
下载:https://www.mongodb.com/products/tools/compass
选择Windows版本:
双击启动:
进行连接:
Importing and exporting data with Compass
Now, we cannot have even a vague idea of what we can or cannot do with our data if we don’t have any data to begin with. In the GitHub repository of the book, in the chapter2 folder, you will find a comma-separated values (CSV) file called cars_data.csv.
现在,如果我们一开始没有任何数据,我们甚至不能有一个模糊的概念,我们可以或不可以用我们的数据做什么。在本书的GitHub存储库中,在chapter2文件夹中,您将找到一个名为cars_data.csv的逗号分隔值(CSV)文件。
Click on the Create Database button and insert the database name carsDB and the collection name cars, as follows.
单击Create Database按钮并插入数据库名称carsDB和集合名称cars,如下所示。
After this step, a new database should be available in the left-hand menu, called carsDB. Select this database on the left and you will see that we created a collection called cars. In fact, we connot have a database without collections. There is a big Import Data button in the middle, and you will use it to open a dialog as follows.
在此步骤之后,左侧菜单中应该有一个名为carsDB的新数据库。选择左边的这个数据库,您将看到我们创建了一个名为cars的集合。事实上,没有集合就没有数据库。中间有一个大的Import Data按钮,您将使用它打开一个对话框,如下所示。
After hiting the Import Data button, locate the previously downloaded CSV file and you will be presented with the opportunity to tune the types of the individual columns as follows.
单击Import Data按钮后,找到之前下载的CSV文件,您将有机会按照如下方式调整各个列的类型。
This is important, especially because we’re importing initial data and we do not want to have integers or floating numbers being interpreted as strings. The MongoDB drivers, such as Motor and PyMongo, that we will be using are “smart” enough to figure out the appropriate data types; however, when dealing with Compasss or similar GUI database tools, it is impreative that you take the time to examine all of the data columns and select the appropriate data types.
这一点很重要,特别是因为我们正在导入初始数据,并且我们不希望将整数或浮点数解释为字符串。我们将使用的MongoDB驱动程序(如Motor和PyMongo)足够“智能”,可以找出适当的数据类型;但是,在使用compass或类似的GUI数据库工具时,您必须花时间检查所有数据列并选择适当的数据类型。
This particular file that we imported contains data about 7323 cars and the default for all the fields is string. We made the following modifications when importing:
- Set the columns year,price,km,kW,cm3, and standard to Number
- Set the imported and registered columns to Boolean
我们导入的这个特定文件包含7323辆汽车的数据,所有字段的默认值都是string。我们在导入时做了如下修改:
- 将“year”、“price”、“km”、“kW”、“cm3”和“standard”列设置为“Number”
- 将imported 和imported 的列设置为Boolean
The names of the columns are pretty self-explanatory, but we will examine them more later. Now, once you hit the Import button, you should have a pretty decent collection with a little over 7000 documents, each having an identical structure that we believe will facilitate the understanding of the operations that we are going to perform later on.
列的名称是不言自明的,但我们将在后面对它们进行更多的研究。现在,单击Import按钮后,您应该有一个相当不错的集合,其中包含7000多个文档,每个文档都具有相同的结构,我们相信这将有助于理解我们稍后要执行的操作。
Later, we will see how we can use Compass to run queries and aggregations and export the data in CSV or JSON formats in a pretty similar way to the import that we just did. We suggest that you play around with the interface and experiment a bit. You can always delete the collection and the database, and then redo our data import from the CSV file from the repository.
稍后,我们将看到如何使用Compass运行查询和聚合,并以CSV或JSON格式导出数据,其方式与我们刚才所做的导入非常相似。我们建议您玩周围的界面和实验一点。您总是可以删除集合和数据库,然后从存储库的CSV文件中重新导入数据。