如何在Mongo Shell中为要求字符串数组的模式添加模式验证?

3

我打算在Mongo Shell上运行以下命令,一旦我完善了它所需要的集合。然而,我无法弄清楚如何处理我所需属性列表中的最后一项,因为它是唯一一个数组。具体来说,它是一个字符串数组。它是最后一项,即imageIDs属性。我放置了enum,但我认为那不对。我该如何要求其类型为字符串数组?

    db.runCommand( {
   collMod: "CustomerOrders",
   validator: { $jsonSchema: {
      bsonType: "object",
      required: [ "dateTime", "restaurantName", "restaurantCity", "restaurantCountry", "contactName", "contactPhone", "contactEmail", "menuSize", "pricePaid", "currentLanguage", "targetLanguage", "imageIDs" ],
      properties: {
         dateTime: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         restaurantName: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         restaurantCity: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         restaurantCountry: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         contactName: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         contactPhone: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         contactEmail: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         menuSize: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         pricePaid: {
            bsonType: "double",
            description: "must be a string and is required"
         },
         currentLanguage: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         targetLanguage: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         imageIDs: {
            bsonType: "enum",
            description: "can only be one of the enum values and is required"
         }
      }
   } },
   validationLevel: "strict"
} )
1个回答

9

对于需要是字符串数组的属性imageIDs,我根据以下规范更改了模式:

 imageIDs: {
    bsonType: "array",
    description: "must be an array and is required",
    minItems: 1,
    maxItems: 25,
    items: {
       bsonType: "string",
    }
 }

老实说,MongoDB文档有些令人疲惫,并且缺乏示例。这个关于JSON schema属性的页面还算有点帮助。点击此处。我是通过不断尝试来确定哪些内容被拒绝,哪些是正确的,从而找到正确的方法的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接