是否可以将MongoDb验证器设置为不插入未定义的属性?

是否可以将MongoDb验证器设置为不插入未定义的属性?,mongodb,schema,jsonschema,Mongodb,Schema,Jsonschema,我正在尝试为我的RESTAPI使用MongoDB实现一个数据库,该数据库能够存储和检索具有特定字段的文档 我可以很容易地使用mongoose,但我希望使用MongoDB的本地驱动程序,因为我想学习MongoDB而不是mongoose { "$jsonSchema": { "bsonType": "object", "required": [ "name", "email" ],

我正在尝试为我的RESTAPI使用MongoDB实现一个数据库,该数据库能够存储和检索具有特定字段的文档

我可以很容易地使用mongoose,但我希望使用MongoDB的本地驱动程序,因为我想学习MongoDB而不是mongoose

    {
      "$jsonSchema": {
       "bsonType": "object",
       "required": [
          "name",
          "email"
        ],
        "properties": {
          "name": {
            "bsonType": "string"
          },
          "email": {
          "bsonType": "string"
          },
          "profileImagePath": {
            "bsonType": "string"
          },
          "blogs": {
            "bsonType": ["object"]
          }
        }
      }
    }
我希望只能插入以下数据:

   "name" : "john",
   "email" : "john@gmail.com"

但不是

   "name" : "john",
   "email" : "john@gmail.com",
   "height" : "5'11"

因为属性中没有指定高度。

所以我已经阅读了MongoDB的文档,可以通过更改
additionalProperties
属性的默认值来指定是否可以将其他属性插入数据库

文件说明

如果为true,则允许其他字段。如果为假,则不是。如果指定了有效的JSON模式对象,则必须根据该模式验证其他字段

默认值是真的

为了避免插入任何附加属性,您只需像这样添加
additionalProperties
属性

{
  "$jsonSchema": {
   "bsonType": "object",
   "required": [
      "name",
      "email"
    ],
    "properties": {
      "name": {
        "bsonType": "string"
      },
      "email": {
      "bsonType": "string"
      },
      "profileImagePath": {
        "bsonType": "string"
      },
      "blogs": {
        "bsonType": ["object"]
      }
    },
    "additionalProperties": false
  }
}
{
  "$jsonSchema": {
   "bsonType": "object",
   "required": [
      "name",
      "email"
    ],
    "properties": {
      "name": {
        "bsonType": "string"
      },
      "email": {
      "bsonType": "string"
      },
      "profileImagePath": {
        "bsonType": "string"
      },
      "blogs": {
        "bsonType": ["object"]
      }
    },
    "additionalProperties": false
  }
}