AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

既存のドキュメントの検証レベルを指定する

検証を追加する前にコレクションにすでに存在するドキュメントの場合、MongoDB がこれらのドキュメントに検証ルールを適用する方法を指定できます。

スキーマのvalidationLevelによって、MongoDB が検証ルールを適用するドキュメントが決まります。

検証レベル
動作

strict

(デフォルト)MongoDB はすべてのドキュメントの挿入とアップデートに同じ検証ルールを適用します。

moderate

MongoDBは、検証ルールに一致する既存の有効なドキュメントの挿入とアップデートに同じ検証ルールを適用します。 検証ルールに一致しないコレクション内の既存のドキュメントの更新は、検証に合格する必要はありません。

このページの例では、次のドキュメントを含むcontactsコレクションを使用しています。

db.contacts.insertMany([ { "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" }, { "_id": 2, "name": "Ivan", "city": "Vancouver" }])

次の例では、 contactsコレクションにstrict検証を追加し、無効なドキュメントを更新しようとした場合の結果を表示します。

1

strict validationLevelを使用してcontactsコレクションにバリデーターを追加する:

db.runCommand( { collMod: "contacts", validator: { $jsonSchema: { bsonType: "object", required: [ "phone", "name" ], properties: { phone: { bsonType: "string", description: "phone must be a string and is required" }, name: { bsonType: "string", description: "name must be a string and is required" } } } }, validationLevel: "strict"} )

validationLevelはstrictであるため、いずれかのドキュメントが更新されると、MongoDB はそのドキュメントを検証するためにそのドキュメントをチェックします。

2

次のアップデート コマンドは、 contactsコレクション内の両方のドキュメントを変更して、いずれのドキュメントもnameを string にする必要がある検証ルールと一致しないようにします。

db.contacts.updateOne( { _id: 1 }, { $set: { name: 10 } }) db.contacts.updateOne( { _id: 2 }, { $set: { name: 20 } })
3

両方の更新操作が失敗します。 MongoDB は、各操作に対して次の出力を返します。

MongoServerError: Document failed validation Additional information: { failingDocumentId: <id>, details: { operatorName: '$jsonSchema', schemaRulesNotSatisfied: [ { operatorName: 'properties', propertiesNotSatisfied: [ { propertyName: 'name', description: 'name must be a string and is required', details: [ { operatorName: 'bsonType', specifiedAs: { bsonType: 'string' }, reason: 'type did not match', consideredValue: <value>, consideredType: 'int' } ] } ] }, { operatorName: 'required', specifiedAs: { required: [ 'phone', 'name' ] }, missingProperties: [ 'phone' ] } ] } }

次の例では、 contactsコレクションにmoderate検証を追加し、無効なドキュメントを更新しようとした場合の結果を表示します。

1

moderate validationLevelを使用してcontactsコレクションにバリデーターを追加する:

db.runCommand( { collMod: "contacts", validator: { $jsonSchema: { bsonType: "object", required: [ "phone", "name" ], properties: { phone: { bsonType: "string", description: "phone must be a string and is required" }, name: { bsonType: "string", description: "name must be a string and is required" } } } }, validationLevel: "moderate"} )

validationLevelはmoderateであるため、

  • _id: 1を使用してドキュメントを更新すると、既存のドキュメントが検証要件を満たしているため、MongoDB は新しい検証ルールを適用します。

  • _id: 2を使用してドキュメントを更新すると、既存のドキュメントが検証要件を満たしていないため、MongoDB は新しい検証ルールを適用しません。

2

次のアップデート コマンドは、 contactsコレクション内の両方のドキュメントを変更して、いずれのドキュメントもnameを string にする必要がある検証ルールと一致しないようにします。

db.contacts.updateOne( { _id: 1 }, { $set: { name: 10 } }) db.contacts.updateOne( { _id: 2 }, { $set: { name: 20 } })
3

MongoDB は、各操作に対して次の出力を返します。

// _id: 1 MongoServerError: Document failed validationAdditional information: { failingDocumentId: 1, details: { operatorName: '$jsonSchema', schemaRulesNotSatisfied: [ { operatorName: 'properties', propertiesNotSatisfied: [ { propertyName: 'name', description: 'name must be a string and is required', details: [ { operatorName: 'bsonType', specifiedAs: { bsonType: 'string' }, reason: 'type did not match', consideredValue: 10, consideredType: 'int' } ] } ] } ] }} // _id: 2 { acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 0, upsertedCount: 0}

出力には次の内容が含まれます。

  • _id: 1を含むドキュメントの更新は失敗します。 このドキュメントは初期検証要件を満たしており、MongoDB はこのドキュメントに検証ルールを適用します。

  • _id: 2を含むドキュメントの更新は成功します。 このドキュメントは初期検証要件を満たしていなかったため、MongoDB はこのドキュメントに検証ルールを適用しません。

重要

エラー出力は人間が消費するためのものです。 将来変更される可能性があるため、スクリプトでは依存しないでください。