8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

22.2 文档存储概念22.2 Document Store Concepts

本节解释了使用 MySQL 作为文档存储时引入的概念。This section explains the concepts introduced as part of using MySQL as a document store.

JSON 文档JSON Document

JSON 文档是一种由键值对组成的数据结构,是使用 MySQL 作为文档存储的基本结构。例如,本章后面安装的 world_x 模式包含如下文档:A JSON document is a data structure composed of key-value pairs and is the fundamental structure for using MySQL as document store. For example, the world_x schema (installed later in this chapter) contains this document:

{
    "GNP": 4834,
    "_id": "00005de917d80000000000000023",
    "Code": "BWA",
    "Name": "Botswana",
    "IndepYear": 1966,
    "geography": {
        "Region": "Southern Africa",
        "Continent": "Africa",
        "SurfaceArea": 581730
    },
    "government": {
        "HeadOfState": "Festus G. Mogae",
        "GovernmentForm": "Republic"
    },
    "demographics": {
        "Population": 1622000,
        "LifeExpectancy": 39.29999923706055
    }
}

该文档显示,键的值可以是简单的数据类型,例如整数或字符串,但也可以包含其他文档、数组和文档列表。例如,geography 键的值包含多个键值对。JSON 文档在内部使用 MySQL 二进制 JSON 对象表示,通过 JSON MySQL 数据类型。This document shows that the values of keys can be simple data types, such as integers or strings, but can also contain other documents, arrays, and lists of documents. For example, the geography key's value consists of multiple key-value pairs. A JSON document is represented internally using the MySQL binary JSON object, through the JSON MySQL datatype.

文档与传统关系数据库中已知表的最重要区别在于,文档的结构不必事先定义,而且集合可以包含多个结构不同的文档。另一方面,关系表要求其结构必须定义,并且表中的所有行必须包含相同的列。The most important differences between a document and the tables known from traditional relational databases are that the structure of a document does not have to be defined in advance, and a collection can contain multiple documents with different structures. Relational tables on the other hand require that their structure be defined, and all rows in the table must contain the same columns.

集合Collection

集合是用于在 MySQL 数据库中存储 JSON 文档的容器。应用程序通常对文档集合执行操作,例如查找特定文档。A collection is a container that is used to store JSON documents in a MySQL database. Applications usually run operations against a collection of documents, for example to find a specific document.

CRUD 操作CRUD Operations

对集合可以执行的四个基本操作是创建、读取、更新和删除(CRUD)。在 MySQL 中,这意味着:The four basic operations that can be issued against a collection are Create, Read, Update and Delete (CRUD). In terms of MySQL this means:

  • 创建一个新文档(插入或添加)Create a new document (insertion or addition)

  • 读取一个或多个文档(查询)Read one or more documents (queries)

  • 更新一个或多个文档Update one or more documents

  • 删除一个或多个文档Delete one or more documents