Om du använder mongo version 3.6 kan du aktivera schemavalidering med $jsonSchema
på serversidan
varje dokument vi uppdaterar/infogar kommer att valideras mot valideringsschemat, om valideringen misslyckas kommer ett fel att visas och inga ändringar kommer att göras på dokumentet
exempel node
samlingsschema
{
_id : string,
parent : [string, null],
children : string[2]
}
valideringsschema
db.createCollection("node", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "_id" ],
properties: {
parent: {
bsonType: ["string", "null"],
description: "must be a string"
},
children: {
bsonType: ["array"],
items : { bsonType: ["string"] },
minItems: 0,
maxItems: 2,
description: "must be a array of string and max is 2"
}
}
}
}
});
infogar [med giltiga dokument]
> db.node.insert( { _id: "Books", children: [ "Programming" ], parent: null } )
WriteResult({ "nInserted" : 1 })
> db.node.insert( { _id: "Programming", children: [ "Databases", "Languages" ], parent: "Books" } )
WriteResult({ "nInserted" : 1 })
> db.node.insert( { _id: "Languages", children: [ ], parent: "Programming" } )
WriteResult({ "nInserted" : 1 })
> db.node.insert( { _id: "Databases", children: [ "MongoDB", "dbm" ], parent: "Programming" } )
WriteResult({ "nInserted" : 1 })
> db.node.insert( { _id: "MongoDB", children: [ ], parent: "Databases" } )
WriteResult({ "nInserted" : 1 })
> db.node.insert( { _id: "dbm", children: [ ], parent: "Databases" } )
WriteResult({ "nInserted" : 1 })
>
hitta
> db.node.find()
{ "_id" : "Books", "children" : [ "Programming" ], "parent" : null }
{ "_id" : "Programming", "children" : [ "Databases", "Languages" ], "parent" : "Books" }
{ "_id" : "Languages", "children" : [ ], "parent" : "Programming" }
{ "_id" : "Databases", "children" : [ "MongoDB", "dbm" ], "parent" : "Programming" }
{ "_id" : "MongoDB", "children" : [ ], "parent" : "Databases" }
{ "_id" : "dbm", "children" : [ ], "parent" : "Databases" }
infoga med ogiltigt dokument [barnstorlek> 2]
> db.node.insert({_id : "1", children : ["c1", "c2", "c3"], parent : "p1"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
>
insättningen misslyckades med ett valideringsfel
uppdatering - försöker lägga till 3:e underordnade för _id-databaser, misslyckades med valideringsfel
> db.node.updateOne( { _id: "Databases"}, {$push : {children: [ "Oracle" ]}} )
2018-02-25T21:00:08.087+0530 E QUERY [thread1] WriteError: Document failed validation :
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"q" : {
"_id" : "Databases"
},
"u" : {
"$push" : {
"children" : [
"Oracle"
]
}
},
"multi" : false,
"upsert" : false
}
})
[email protected]/mongo/shell/bulk_api.js:466:48
Bulk/[email protected]/mongo/shell/bulk_api.js:846:49
Bulk/[email protected]/mongo/shell/bulk_api.js:910:13
Bulk/[email protected]/mongo/shell/bulk_api.js:1154:21
[email protected]/mongo/shell/crud_api.js:572:17
@(shell):1:1
>
se schemavalidering &jsonSchema för fler alternativ, lägga till validering till befintlig insamling och hantering av valideringsfel