Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB验证失败_Mongodb - Fatal编程技术网

MongoDB验证失败

MongoDB验证失败,mongodb,Mongodb,我使用了MongoDB的验证规则,但是每次插入都会失败 db.createCollection("mycollection",{ validator:{ $and:[ {name: {$type:"string"}}, {age: {$type:"int"}} ] }}) 添加条目时 db.mycollection.insertOne({name:"Joseph", age: 18}) 但我总是得到一个错误,一个通用消息文档验证失败。

我使用了MongoDB的验证规则,但是每次插入都会失败

db.createCollection("mycollection",{
validator:{
    $and:[ 
        {name: {$type:"string"}}, 
        {age: {$type:"int"}}   
    ]
}})
添加条目时

db.mycollection.insertOne({name:"Joseph", age: 18})
但我总是得到一个错误,一个通用消息文档验证失败。 有什么想法吗?
谢谢。

在mongodb中,int的默认类型是Double。 所以,您可以在验证规则中设置类型Double,或者尝试

db.mycollection.insertOne({name:"Joseph", age: NumberInt(18)}))

在mongodb中,int的默认类型是Double。 所以,您可以在验证规则中设置类型Double,或者尝试

db.mycollection.insertOne({name:"Joseph", age: NumberInt(18)}))

非常感谢。MongoDB的决定毫无意义,因为Number.isInteger18是正确的。谢谢!MongoDB的这个决定毫无意义,因为Number.isInteger18是真的。