【发布时间】:2020-06-11 11:48:43
【问题描述】:
我设法创建了一个 mongodb 集合:
db.createCollection('product', {
validator: {
$jsonSchema: {
bsonType: "object",
required: ['product_name', 'num'],
properties: {
product_name: {
bsonType: "string",
description: "must be a string"
},
num: {
minimum: 0,
bsonType: "int", // problem place
description: "must be a int and large than 0"
}
}
}
},
validationLevel: "moderate"
})
并插入一个文档:
db.product.insertOne({product_name:'baseball',num:10})
但它不起作用,这是错误:
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5ee2159b317a66d955a39c54"),
"product_name" : "baseball",
"num" : 10
}
})
好像我这样创建“产品”col(使用类型:“数字”,而不是bsonType):
properties: {
product_name: {
type: "string",
description: "must be a string"
},
num: {
minimum: 0,
type: "number", //changed here
description: "must be a int and large than 0"
}
}
插入操作工作,为什么?
【问题讨论】:
标签: mongodb jsonschema