这对我来说有点奇怪,我是Mongo db的新手。
我通过nosqlboster控制台设置以下验证规则:
let validator = [
{language : {
$type : "string",
$exists : true
}},
{label : {
$type : "string",
$exists : true
}},
{text : {
$type : "string",
$exists : true
}},
{order : {
$type : "int",
$exists : true
}}
];
db.runCommand( {
collMod: "profile",
validator,
validationLevel: "moderate", //off | strict
//validationAction: "warn" |"error"
})
好吧,添加验证器后,我尝试添加以下文档:
db.profile.insert({language: 'en', label: 'Born', text: '31 Dec 1983 - Jaén, Spain', order: 1})
我假装是为了使所有这些领域都必须进行。甚至提供所有这些类型并拥有适当的类型,我总是会遇到以下插入错误:
{
"message" : "write failed with error: {" +
" 'nInserted' : 0," +
" 'writeError' : {" +
" t'code' : 121," +
" t'errmsg' : 'Document failed validation'" +
" }" +
"}",
"stack" : "script:1:96" +
"script:1:96" +
"script:1:96" +
"script:1:96",
"code" : {
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0
}
}
现在变得好奇了...我在做什么错?
感谢您的时间
我解决了我的问题。我只需要numberInt()函数即可包装整数。否则,它被认为是浮点数。以下作用如预期:
db.profile.insert({language: 'en', label: 'Born', text: '31 Dec 1983 - Jaén, Spain', order: NumberInt(1)})
谢谢大家!
这是设计。
详细信息
The mongo shell treats all numbers as floating-point values by default.
The mongo shell provides the NumberInt() constructor to explicitly specify 32-bit integers.
来源:https://docs.mongodb.com/manual/core/shell-types/#numberint