Mit collMod
können Sie ein JSON-Schema auf eine vorhandene Sammlung anwenden Befehl zum Hinzufügen eines neuen JSON-Schemas zur Sammlung https://docs.mongodb. com/manual/core/schema-validation/
. Ein Beispiel unten. Es gilt jedoch nur für neue Schreibvorgänge, es wird nicht für vorhandene Dokumente in der Sammlung ausgeführt.
db.runCommand( {
collMod: "contacts",
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone", "name" ],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
name: {
bsonType: "string",
description: "must be a string and is required"
}
}
} },
validationLevel: "moderate"
} )