2017-05-04 56 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