【发布时间】:2020-07-25 01:27:15
【问题描述】:
我是MongoDB 的新手,我已经写了我的schema validation 但我无法insert 数据给我schema validation error,代码如下所示,任何帮助将不胜感激。
验证者:
db.createCollection("kartCollection", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "uid", "items" ],
properties: {
uid: {
bsonType: "number",
description: "holds user id"
},
items: {
bsonType: "object",
required: [ "itemid","qty","qty_type","cost","currency" ],
properties: {
itemid: {
bsonType: "number",
description: "holds items id"
},
qty: {
bsonType: "number",
"description": "holds quantity of item"
},
qty_type: {
bsonType: "string",
"description": "holds quantity type like l,kl,kg gm,pcs"
},
cost: {
bsonType: "number",
"description": "holds cost of the selected item for quiker fetch"
},
currency: {
bsonType: "string",
"description": "holds currency of the item sold in "
}
}
}
}
}
}
});
db.kartCollection.insert({uid:'1',items:{{itemid:'1',qty:'1',qty_type:'kg',cost:'20',currency:'INR'}}});
错误
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
【问题讨论】:
标签: mongodb validation schema