在MongoDB中验证数组项的类型

时间:2018-11-07 07:07:46

标签: mongodb validation bson

我正在使用MongoDB 4.0。

我的收藏集中的文档可以有一个numbers字段,该字段包含一个整数数组。通过使用下面的代码,我们可以验证numbers确实是一个数组,但是可以确保它们是整数吗?

properties: {
  numbers: {
    bsonType: 'array'
  }
 }

我看过validation docsBSON type docs,但其中任何一个都没说什么。一个示例显示了一个用方括号(["double"])包围的字段,但是该类型并未描述为数组,因此添加方括号似乎没有效果。

1 个答案:

答案 0 :(得分:1)

似乎还有一个属性items,您可以在其中为数组中的项目定义方案

properties: {
  numbers: {
    bsonType: 'array'
    items: {
      bsonType: 'int'
    }
  }
}

此属性似乎为be from mongo 3.6