You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB创建JSON Schema时integer类型不支持报错求助

问题解决:MongoDB JSON Schema使用integer类型报错

MongoDB 4.4及更早版本的JSON Schema校验实现里,不支持单独的integer类型,所有数值类型统一用number来涵盖,这就是你遇到'$jsonSchema type 'integer' is not currently supported'错误的核心原因。

解决办法很直接:

  • 保留number类型,通过添加multipleOf: 1约束来实现整数校验,既符合MongoDB的Schema规则,又能确保字段值为整数。

示例校验Schema:

{
  "$jsonSchema": {
    "bsonType": "object",
    "properties": {
      "age": {
        "bsonType": "number",
        "multipleOf": 1,
        "description": "必须是整数类型"
      }
    }
  }
}

使用这个修改后的Schema创建集合,不管是通过驱动还是MongoDB Compass操作,都能正常生效,同时达到校验整数的目的。

内容的提问来源于stack exchange,提问作者Codrin Sirboiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 10:27:28