FastAPI+React全栈开发08 安装MongoDB

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

拉取镜像:

bash 复制代码
docker pull mongo:6.0.14-jammy

创建容器:

bash 复制代码
docker run --name mongo -d --restart=always -p 27017:27017 mongo:6.0.14-jammy

之后使用python进行连接测试:

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文件中重新导入数据。

相关推荐
编程老船长10 小时前
第26章 Java操作Mongodb实现数据持久化
数据库·后端·mongodb
zqx_717 小时前
随记 前端框架React的初步认识
前端·react.js·前端框架
fishmemory7sec20 小时前
Koa2项目实战2(路由管理、项目结构优化)
数据库·mongodb·koa
缘友一世1 天前
macos安装mongodb
数据库·mongodb·macos
TonyH20021 天前
webpack 4 的 30 个步骤构建 react 开发环境
前端·css·react.js·webpack·postcss·打包
掘金泥石流2 天前
React v19 的 React Complier 是如何优化 React 组件的,看 AI 是如何回答的
javascript·人工智能·react.js
lucifer3112 天前
深入解析 React 组件封装 —— 从业务需求到性能优化
前端·react.js
攻城狮numberOne2 天前
MongoDB集群模式详解及应用实战
数据库·mongodb
姜豆豆耶2 天前
MongoDB-aggregate流式计算:带条件的关联查询使用案例分析
数据库·mongodb