2017-05-04 57 views
0

我是mongodb上的新手。我需要您对mongodb文档验证的帮助。我想创建下面的MongoDB的文档结构:Mongodb文件验证

db.createCollection("People2", { 
    validator: {`enter code here` 
    $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: "[email protected]", 
      UserVerification: "Validated" 
     } 
); 

但得到这个错误:

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

我认为,问题来了来自对象的“身份”验证声明是不正确的。

你的帮助会提前welcome.`

感谢的

回答

0

请试试这个,你的发言是使用IdentityCard但不认同。

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" ] } 
     } 
    ] 
    } 
}) 
+0

嗨赫尔曼,非常感谢你。正确工作。 – mamkha