Automattic/mongoose · error · MongooseError

encryptionType of a nested schema must match the encryption

Error message

encryptionType of a nested schema must match the encryption type of the parent schema.

What it means

Error "encryptionType of a nested schema must match the encryption type of the parent schema." thrown in Automattic/mongoose.

Source

Thrown at lib/schema.js:916

      } else {
        // Either the type is non-POJO or we interpret it as Mixed anyway
        if (prefix) {
          this.nested[prefix.substring(0, prefix.length - 1)] = true;
        }
        this.path(prefix + key, val);
        if (!val?.instanceOfSchema && utils.isPOJO(val?.discriminators)) {
          const schemaType = this.path(prefix + key);
          for (const key in val.discriminators) {
            schemaType.discriminator(key, val.discriminators[key]);
          }
        }
      }
    }

    if (val.instanceOfSchema && val.encryptionType() != null) {
      // schema.add({ field: <instance of encrypted schema> })
      if (this.encryptionType() != val.encryptionType()) {
        throw new MongooseError('encryptionType of a nested schema must match the encryption type of the parent schema.');
      }

      for (const [encryptedField, encryptedFieldConfig] of Object.entries(val.encryptedFields)) {
        const path = fullPath + '.' + encryptedField;
        this._addEncryptedField(path, encryptedFieldConfig);
      }
    } else if (typeof val === 'object' && 'encrypt' in val) {
      // schema.add({ field: { type: <schema type>, encrypt: { ... }}})
      const { encrypt } = val;

      if (this.encryptionType() == null) {
        throw new MongooseError('encryptionType must be provided');
      }

      this._addEncryptedField(fullPath, encrypt);
    } else {
      // if the field was already encrypted and we re-configure it to be unencrypted, remove
      // the encrypted field configuration

View on GitHub (pinned to 49cdab0136)

When it happens

Trigger: Thrown at lib/schema.js:916 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/ba83494b3da27c15. Report an issue: GitHub.