Database Manual / Security / Encryption / In-Use Encryption / Queryable Encryption / Fundamentals

Create an Encryption Schema创建加密架构

About this Task关于此任务

To make encrypted fields queryable, create an encryption schema. This schema defines which fields are queryable, and which query types are permitted. 要使加密字段可查询,请创建加密模式。此模式定义了哪些字段是可查询的,以及允许哪些查询类型。For more information, see Encrypted Fields and Enabled Queries.有关更多信息,请参阅加密字段和已启用查询。

Important

Queryable Encryption supports equality and range queries. You can configure a field for only one query type.可查询加密支持相等和范围查询。您只能为一种查询类型配置字段。

Before you Begin开始之前

When you make encrypted fields queryable, consider performance and security. 当您使加密字段可查询时,请考虑性能和安全性。For details on how each configuration option affects these, see Configure Encrypted Fields for Optimal Search and Storage.有关每个配置选项如何影响这些的详细信息,请参阅配置加密字段以实现最佳搜索和存储。

Steps步骤

1

Create the encryption schema.创建加密架构。

Include an encryptedFieldsObject with a nested fields array:包含一个带有嵌套fields数组的encryptedFieldsObject

const encryptedFieldsObject = {
fields: []
}
2

Specify fields to encrypt.指定要加密的字段。

  1. Add the path and bsonType strings to a document within the fields array:pathbsonType字符串添加到字段数组中的文档中:

    const encryptedFieldsObject = {
    fields: [
    {
    path: "myDocumentField",
    bsonType: "int"
    }
    ]
    }

    Important

    You can specify any field for encryption except the _id field.您可以指定除_id字段之外的任何加密字段。

  2. Optionally, set a keyId field with the DEK ID.(可选)使用DEK ID设置keyId字段。

    Important

    Key IDs must be unique, otherwise the server returns an error.键ID必须是唯一的,否则服务器将返回错误。

    By configuring AutoEncryptionSettings on the client, you can use the createEncryptedCollection helper method to create keys automatically.通过在客户端配置AutoEncryptionSettings,您可以使用createEncryptedCollection辅助方法自动创建键。

    {
    path: "myDocumentField",
    bsonType: "int",
    keyId: "<unique data encryption key>"
    }
3

Enable equality queries on desired fields.在所需字段上启用相等查询。

This enables querying with the $eq, $ne, $in, and $nin operators.这允许使用$eq$ne$in$nin运算符进行查询。

Add the queries object and set queryType to "equality":添加queries对象并将queryType设置为"equality"

{
path: "myDocumentField",
bsonType: "int",
queries: { queryType: "equality" }
}
4

Enable range queries on desired fields.在所需字段上启用范围查询。

This enables querying with the $lt, $lte, $gt, and $gte operators.这允许使用$lt$lte$gt$gte运算符进行查询。

For details on how the following options affect security and performance, see Configure Encrypted Fields for Optimal Search and Storage.有关以下选项如何影响安全性和性能的详细信息,请参阅配置加密字段以实现最佳搜索和存储

  1. Add the queries object and set queryType to "range":添加queries对象并将queryType设置为"range"

    {
    path: "myDocumentRangeField",
    bsonType: "int",
    queries: { queryType: "range" }
    }
  2. Set the following fields:设置以下字段:

    Field字段Type类型Description描述
    min and maxSame as field bsonType

    Required if bsonType is decimal or double. 如果bsonType为十进制或双精度,则为必填项。Optional but highly recommended if it is int, long, or date. Defaults to the minimum and maximum values of the bsonType.可选,但强烈建议使用intlongdate。默认为bsonType的最小值和最大值。

    When possible, specifying bounds on a query improves performance. If querying values outside of these inclusive bounds, MongoDB returns an error.如果可能,在查询上指定边界可以提高性能。如果查询这些包含边界之外的值,MongoDB将返回错误。

    {
    path: "myDocumentRangeField",
    bsonType: "int",
    queries: { queryType: "range",
    min: 0,
    max: 1200
    }
    }
5

Enable prefix, suffix, or substring queries on desired fields.在所需字段上启用前缀、后缀或子字符串查询。

These query types are for string fields only. You can enable both prefixPreview and suffixPreview on the same field, but can't enable either if using substringPreview.这些查询类型仅适用于string字段。您可以在同一字段上同时启用prefixPreviewsuffixPreview,但如果使用subscribeReview,则无法启用两者。

Warning

Prefix, Suffix, and Substring Queries are in Public Preview前缀、后缀和子字符串查询处于公开预览状态

Queryable Encryption prefix, suffix, and substring queries are available in public preview in MongoDB 8.2. Do not enable these query types in production. Public preview functionality will be incompatible with the GA feature, and you will have to drop any collections that enable these queries.MongoDB 8.2的公共预览版中提供了可查询的加密前缀、后缀和子字符串查询。不要在生产环境中启用这些查询类型。公共预览功能将与GA功能不兼容,您必须删除启用这些查询的任何集合。

  1. Add the queries object and set queryType to "prefixPreview", "suffixPreview", or "substringPreview":

    {
    path: "myDocumentStringField",
    bsonType: "string",
    queries: { queryType: "substringPreview" }
    }
  2. Set the following fields.设置以下字段。

    For details on how they affect security and performance, see Configure Encrypted Fields for Optimal Search and Storage.有关它们如何影响安全性和性能的详细信息,请参阅配置加密字段以实现最佳搜索和存储。

    Field字段Type类型Description描述
    strMaxLengthinteger整数substringPreview queries only. The maximum allowed length for a substring-indexed field.仅限查询。子字符串索引字段的最大允许长度。
    strMinQueryLengthinteger整数The minimum allowed prefix/suffix/substring length to query.允许查询的最小前缀/后缀/子字符串长度。
    strMaxQueryLengthinteger整数

    The maximum allowed prefix/suffix/substring length to query.允许查询的最大前缀/后缀/子字符串长度。

    IMPORTANT: This setting strongly impacts query performance. Limit it whenever possible.此设置强烈影响查询性能。尽可能限制它。

    caseSensitiveBoolean布尔值Optional. 可选。Whether queries are case-sensitive. Defaults to true.查询是否区分大小写。默认为true
    diacriticSensitiveBoolean布尔值Optional. 可选。Whether queries are diacritic-sensitive. Defaults to true.查询是否对变音符号敏感。默认为true
    {
    path: "myDocumentStringField",
    bsonType: "string",
    queries: {
    "queryType": "substringPreview",
    "strMaxLength": 30,
    "strMinQueryLength": 1,
    "strMaxQueryLength": 20,
    "caseSensitive": false
    }
    }

Example示例

This example shows how to create an encryption schema for hospital data.此示例显示了如何为医院数据创建加密模式。

Consider the following document that contains personally identifiable information (PII), credit card information, and sensitive medical information:考虑以下包含个人身份信息(PII)、信用卡信息和敏感医疗信息的文档:

{
"firstName": "Jon",
"lastName": "Snow",
"patientId": 12345187,
"address": "123 Cherry Ave",
"medications": [
"Adderall",
"Lipitor"
],
"patientInfo": {
"ssn": "921-12-1234",
"billing": {
"type": "visa",
"number": "1234-1234-1234-1234"
}
}
}

To ensure the PII and sensitive medical information stays secure, this encryption schema adds the relevant fields:为确保PII和敏感医疗信息的安全,此加密模式添加了相关字段:

const encryptedFieldsObject = {
fields: [
{
path: "patientId",
bsonType: "int"
},
{
path: "patientInfo.ssn",
bsonType: "string"
},
{
path: "medications",
bsonType: "array"
},
{
path: "patientInfo.billing",
bsonType: "object"
}
]
}

Adding the queries property makes the patientId and patientInfo.ssn fields queryable. This example enables equality queries:添加queries属性使patientIdpatientInfo.ssn字段可查询。此示例启用相等查询:

const encryptedFieldsObject = {
fields: [
{
path: "patientId",
bsonType: "int",
queries: { queryType: "equality" }
},
{
path: "patientInfo.ssn",
bsonType: "string",
queries: { queryType: "equality" }
},
{
path: "medications",
bsonType: "array"
},
{
path: "patientInfo.billing",
bsonType: "object"
},
]
}