必填字段是否允许值为“null”?

Is the value of `null` allowed for fields that are required?

在 MongoDB 中,集合中的字段具有 required = true 条件是什么意思?

值可以是null吗?

没有值不能是null,如果将null值传递给实现required的字段,required: true将抛出错误。

根据猫鼬 documentation on required

By default, a value satisfies the required validator if val != null (that is, if the value is not null nor undefined)

请记住,这是默认行为,因此可以自定义实现。

required = true 似乎在使用 Mongoose,因此它不接受空值。

By default, a value satisfies the required validator if val != null (that is, if the value is not null nor undefined).

可以在他们的文档中找到:https://mongoosejs.com/docs/api.html#schematype_SchemaType-required

MongoDB 有类似的方法,但是你在 required 选项中声明所有字段

即:

...
required: [ "name", "year", "major", "address" ]
...

可以找到更多 here