8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

22.4.3 文档和集合22.4.3 Documents and Collections

当您将 MySQL 用作文档存储时,集合是模式中的容器,您可以创建、列出和删除它们。集合包含的 JSON 文档可以被添加、查找、更新和删除。When you are using MySQL as a Document Store, collections are containers within a schema that you can create, list, and drop. Collections contain JSON documents that you can add, find, update, and remove.

本节中的示例使用 world_x 模式中的 countryinfo 集合。有关设置 world_x 模式的说明,请参见 第 22.4.2 节,“下载并导入 world_x 数据库”。The examples in this section use the countryinfo collection in the world_x schema. For instructions on setting up the world_x schema, see Section 22.4.2, “Download and Import world_x Database”.

文档Documents

在 MySQL 中,文档以 JSON 对象的形式表示。在内部,它们以高效的二进制格式存储,从而支持快速查询和更新。In MySQL, documents are represented as JSON objects. Internally, they are stored in an efficient binary format that enables fast lookups and updates.

  • Python 的简单文档格式:Simple document format for Python:

    {"field1": "value", "field2" : 10, "field 3": null}
    

文档数组由一组文档组成,各文档之间用逗号分隔,并用 [ 和 ] 字符括起来。An array of documents consists of a set of documents separated by commas and enclosed within [ and ] characters.

  • Python 的简单文档数组:Simple array of documents for Python:

    [{"Name": "Aruba", "Code:": "ABW"}, {"Name": "Angola", "Code:": "AGO"}]
    

MySQL 支持 JSON 文档中的以下 Python 值类型:MySQL supports the following Python value types in JSON documents:

  • 数字(整数和浮点数)numbers (integer and floating point)

  • 字符串strings

  • 布尔值(False 和 True)boolean (False and True)

  • 无None

  • 更多 JSON 值的数组arrays of more JSON values

  • 更多 JSON 值的嵌套(或嵌入)对象nested (or embedded) objects of more JSON values

集合Collections

集合是存储具有共同目的并且可能共享一个或多个索引的文档的容器。每个集合都有一个唯一名称,并存在于单一模式中。Collections are containers for documents that share a purpose and possibly share one or more indexes. Each collection has a unique name and exists within a single schema.

模式(schema)一词等同于数据库,意味着一组数据库对象,而不是用于强制数据结构和约束的关系模式。模式不会强制集合中文档的一致性。The term schema is equivalent to a database, which means a group of database objects as opposed to a relational schema, used to enforce structure and constraints over data. A schema does not enforce conformity on the documents in a collection.

在本快速入门指南中:In this quick-start guide:

  • 基本对象包括:Basic objects include:

    对象形式Object form 说明Description
    dbdb db 是一个全局变量,指向当前活动的模式。当你想对该模式进行操作时,例如检索集合,你可以使用 db 变量提供的方法。db is a global variable assigned to the current active schema. When you want to run operations against the schema, for example to retrieve a collection, you use methods available for the db variable.
    db.get_collections() db.get_collections() 返回模式中的集合列表。使用该列表获取集合对象的引用,对其进行迭代,等等。db.get_collections() returns a list of collections in the schema. Use the list to get references to collection objects, iterate over them, and so on.
  • 按集合范围进行的基本操作包括:Basic operations scoped by collections include:

    操作形式Operation form 说明Description
    db.name.add() add() 方法将一个文档或文档列表插入到指定的集合中。The add() method inserts one document or a list of documents into the named collection.
    db.name.find() find() 方法返回指定集合中的部分或全部文档。The find() method returns some or all documents in the named collection.
    db.name.modify() modify() 方法更新指定集合中的文档。The modify() method updates documents in the named collection.
    db.name.remove() remove() 方法从指定集合中删除一个文档或一组文档。The remove() method deletes one document or a list of documents from the named collection.

相关信息Related Information