Mongodb文档验证

Mongodb文档验证,mongodb,Mongodb,我需要您在mongodb文档验证方面的帮助。我正在尝试创建以下mongodb文档结构: db.createCollection("People2", { validator: { $and: [ { Identity: { Number: { $type: "string", $exists: true }, Ty

我需要您在mongodb文档验证方面的帮助。我正在尝试创建以下mongodb文档结构:

db.createCollection("People2", {
  validator: {
    $and: [
      {
        Identity: {
            Number: { 
                $type: "string", 
                $exists: true  
            },
            Type: { 
               $type: "string", 
               $exists: true 
            }
        }
      },
      {
        "lastName": { 
          $type: "string", 
          $exists: true
        }
      },
      {
        "email": {
          $type: "string",
          $exists: true,
        }
      },
      {
        UserVerification: { $in: [ "Rejected", "Validated" ] }
      }
    ]
  }
})
下面是测试文档插入的命令:

db.People2.insert(
        {
          IdentityCard: { 
             Number: "#1234",
             Type: "typeA"
          },
          lastName: "toto", 
          email: "toto@mydom.com", 
          UserVerification: "Validated"
        }
);
但是有一个错误:

WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 121,
                "errmsg" : "Document failed validation"
        }
})

我认为问题来自对象“标识”验证声明,该声明不正确。

请尝试此操作,您的语句使用的是IdentityCard,而不是Identity

db.createCollection("People2", {
  validator: {
    $and: [
      {
        "IdentityCard.Number": { 
                $type: "string", 
                $exists: true  
            },
         "IdentityCard.Type": { 
               $type: "string", 
               $exists: true 
            }
      },
      {
        "lastName": { 
          $type: "string", 
          $exists: true
        }
      },
      {
        "email": {
          $type: "string",
          $exists: true,
        }
      },
      {
        UserVerification: { $in: [ "Rejected", "Validated" ] }
      }
    ]
  }
})