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

無効なドキュメントの処理方法を選択する

MongoDB が検証ルールに違反するドキュメントをどのように処理するかは指定できます。 操作によって無効なドキュメントが作成される場合、MongoDB は次のいずれかを実行できます。

  • 検証条件に違反する挿入または更新を拒否します。 これはデフォルトの動作です。

  • 操作の続行を許可しますが、違反を MongoDB ログに記録します。

無効なドキュメントを拒否することで、スキーマの一貫性が保たれます。 ただし、スキーマが確立される前のドキュメントを含むデータ移行など、特定のシナリオでは無効なドキュメントを許可する必要がある場合があります。

スキーマのvalidationActionオプションによって、MongoDB が無効なドキュメントを処理する方法が決まります。

検証アクション
動作

error

(デフォルト)MongoDB は、検証条件に違反する挿入または更新を拒否します。

warn

MongoDB は操作の続行を許可しますが、違反を MongoDB ログに記録します。

errorAndLog

MongoDB は検証基準に違反する挿入またはアップデートを拒否し、そのエラーを mongodログファイルに記録します。

バージョン8.1の新機能。

注意

コレクションで errorAndLog 検証アクションを使用している場合、コレクションを削除する まで、またはコレクションの検証アクションを古いバージョンでサポートされている検証アクションに変更するまで、 MongoDB はダウングレードできません。コレクションの検証アクションを変更するには、collMod コマンドを使用します。

次の手順は、無効なドキュメントを拒否するスキーマ検証を作成する方法を示しています。

1

validationAction: "error"を持つ JSON schema バリデーターを使用してcontactsコレクションを作成します。

db.createCollection( "contacts", { validator: { $jsonSchema: { bsonType: "object", required: [ "phone" ], properties: { phone: { bsonType: "string", description: "must be a string and is required" }, email: { bsonType : "string", pattern : "@mongodb\\.com$", description: "must be a string and end with '@mongodb.com'" } } } }, validationAction: "error"} )

error validationActionを使用すると、MongoDB は無効なドキュメントを拒否し、コレクションに挿入されなくなります。

2

次のドキュメントを挿入してみてください。

db.contacts.insertOne( { name: "Amanda", email: "amanda@xyz.com" })

このドキュメントが検証ルールに違反する理由は以下のとおりです。

  • emailフィールドは正規表現パターンに一致しません。 emailフィールドは@mongodb.comで終わる必要があります。

  • 必須のphoneフィールドがありません。

操作は、次のエラーで失敗します。

MongoServerError: Document failed validationAdditional information: { failingDocumentId: ObjectId("6377cca4aac957f2b77ea955"), details: { operatorName: '$jsonSchema', schemaRulesNotSatisfied: [ { operatorName: 'properties', propertiesNotSatisfied: [ { propertyName: 'email', description: "must be a string and end with '@mongodb.com'", details: [ { operatorName: 'pattern', specifiedAs: { pattern: '@mongodb\\.com$' }, reason: 'regular expression did not match', consideredValue: 'amanda@xyz.com' } ] } ] }, { operatorName: 'required', specifiedAs: { required: [ 'phone' ] }, missingProperties: [ 'phone' ] } ] }}

次の手順は、無効なドキュメントを許可し、無効なドキュメントを MongoDB ログに記録するスキーマ検証を作成する方法を示しています。

1

validationAction: "warn"を持つ JSON schema バリデーターを使用してcontacts2コレクションを作成します。

db.createCollection( "contacts2", { validator: { $jsonSchema: { bsonType: "object", required: [ "phone" ], properties: { phone: { bsonType: "string", description: "must be a string and is required" }, email: { bsonType : "string", pattern : "@mongodb\\.com$", description: "must be a string and end with '@mongodb.com'" } } } }, validationAction: "warn"} )

warn validationActionを使用すると、無効なドキュメントをコレクションに挿入できます。 無効なドキュメントは MongoDB ログに記録されます

2

次のドキュメントを挿入してみてください。

db.contacts2.insertOne( { name: "Amanda", email: "amanda@xyz.com" })

このドキュメントが検証ルールに違反する理由は以下のとおりです。

  • emailフィールドは正規表現パターンに一致しません。 emailフィールドは@mongodb.comで終わる必要があります。

  • 必須のphoneフィールドがありません。

3

MongoDB ログを読み取り可能な形式で表示するには、次のコマンドを実行します。

db.adminCommand( { getLog:'global'} ).log.forEach(x => { print(x) })

MongoDB ログには、次のオブジェクトのようなエントリが含まれています。

{ "t": { "$date": "2022-11-18T13:30:43.607-05:00" }, "s": "W", "c": "STORAGE", "id": 20294, "ctx": "conn2", "msg": "Document would fail validation", "attr": { "namespace": "test.contacts2", "document": { "_id": { "$oid": "6377cf53d59841355cac1cd0" }, "name": "Amanda", "email": "amanda@xyz.com" }, "errInfo": { "failingDocumentId": { "$oid": "6377cf53d59841355cac1cd0" }, "details": { "operatorName": "$jsonSchema", "schemaRulesNotSatisfied": [{ "operatorName": "properties", "propertiesNotSatisfied": [{ "propertyName": "email", "description": "must be a string and end with '@mongodb.com'", "details": [{ "operatorName": "pattern", "specifiedAs": { "pattern": "@mongodb\\.com$" }, "reason": "regular expression did not match", "consideredValue": "amanda@xyz.com" }] }] }, { "operatorName": "required", "specifiedAs": { "required": ["phone"] }, "missingProperties": ["phone"] }] } } }}