Docs Menu
Docs Home
/
MongoDBマニュアル
/ /

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

項目一覧

  • Context
  • 前提条件
  • 手順: strict検証を使用する
  • 手順: moderate検証を使用する
  • 詳細

検証を追加する前にコレクションにすでに存在するドキュメントの場合、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"
} )

validationLevelstrictであるため、いずれかのドキュメントが更新されると、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"
} )

validationLevelmoderateであるため、

  • _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 validation
Additional 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 はこのドキュメントに検証ルールを適用しません。

重要

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

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

  • スキーマ検証を変更する

戻る

クエリ演算子の指定