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

既存の検証ルールを表示する

項目一覧

  • 前提条件
  • 例: db.getCollectionInfos()の構文
  • 例: listCollections構文
  • 詳細

コレクションの検証ルールを表示して、ドキュメントに適用される制限と、無効なドキュメントが発生したときに MongoDB が無効なドキュメントをどのように処理するかを決定できます。

コレクションの検証ルールを表示するには、 db.getCollectionInfos()メソッドまたはlistCollectionsデータベースコマンドを使用します。

どちらのコマンドも同じ情報を返しますが、出力形式は各コマンド間で異なります。

このページの例を実行するには、検証ルールを使用してstudentsコレクションを作成します。 詳細については、「 JSON schema検証の指定 」を参照してください。

次のコマンドは、 db.getCollectionInfos()を使用してstudentsコレクションの検証ルールを返します。

db.getCollectionInfos( { name: "students" } )[0].options.validator

出力は、次の検証オブジェクトのようになります。

{
'$jsonSchema': {
bsonType: 'object',
required: [ 'name', 'year', 'major', 'address' ],
properties: {
name: {
bsonType: 'string',
description: 'must be a string and is required'
},
year: {
bsonType: 'int',
minimum: 2017,
maximum: 3017,
description: 'must be an integer in [ 2017, 3017 ] and is required'
},
gpa: {
bsonType: [ 'double' ],
description: 'must be a double if the field exists'
}
}
}
}

注意

検証アクションとレベルはデフォルトでは含まれていない

validationActionvalidationLevelが明示的に設定されていない場合、 db.getCollectionInfos()はそれらのフィールドを出力に含めません。

次のコマンドは、 listCollectionsを使用してstudentsコレクションの検証ルールを返します。

db.runCommand ( { listCollections: 1, filter: { name: "students" } } )

出力は、次のオブジェクトのようになります。

{
cursor: {
id: Long("0"),
ns: 'test.$cmd.listCollections',
firstBatch: [
{
name: 'students',
type: 'collection',
options: {
validator: {
'$jsonSchema': {
bsonType: 'object',
required: [ 'name', 'year', 'major', 'address' ],
properties: {
name: {
bsonType: 'string',
description: 'must be a string and is required'
},
gpa: {
bsonType: [ 'double' ],
description: 'must be a double if the field exists'
}
}
},
validationAction: 'warn'
}
},
info: {
readOnly: false,
uuid: UUID("bf560865-5879-4ec1-b389-f77a03abbc5a")
},
idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
}
]
},
ok: 1
}

戻る

バイパス