MongoDB文档验证失败

时间:2017-05-13 20:26:19

标签: mongodb validation document

尝试测试以下文档的验证:

db.createCollection("Users", {
  validator: {
    $and: [
      {
        Uid: {
          $type: "string", 
          $exists: true  
        }
      },
      {
        "Doc.Name": { $type: "string", $exists: true},
        "Doc.Files": [
           { 
              "$ref": { $in: [ "fs.files" ] },
              "$id": { $type: "objectId", $exists: true },
              "$db": { $in: [ "mydb" ] }
           }
        ]
       }
    ]
  }
})

有了插入命令:

db.Users.insert({
   Uid: "ABCDFE123m", 
   Doc: { 
     Name: "12hgf123", 
     Files: [
       { 
         $ref:"fs.files", 
         $id: ObjectId("5910e46b24f3f7057c4088a8"), 
         $db: "mydb"
       }, 
       {
         $ref:"fs.files", 
         $id: ObjectId("4010e46b24f3f7057c408806"), 
         $db: "mydb"
        }
     ]
   }
})

我收到以下错误:

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

启用验证文档插入。有人可以帮帮我吗?看起来我忘记了什么。

此致

0 个答案:

没有答案