如何添加架构验证以从 Mongo Shell 获取 MongoDB 中的字符串数组?

tem*_*ame 3 mongodb

一旦我完善了它,我打算在我的 Collection 上的 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"
} )
Run Code Online (Sandbox Code Playgroud)

tem*_*ame 9

对于imageIDs需要是字符串数组的属性,我使用以下规范更改了架构:

 imageIDs: {
    bsonType: "array",
    description: "must be an array and is required",
    minItems: 1,
    maxItems: 25,
    items: {
       bsonType: "string",
    }
 }
Run Code Online (Sandbox Code Playgroud)

老实说,MongoDB 文档有点让人筋疲力尽,而且严重缺乏示例。这个关于 json 模式属性的页面有点帮助。我通过反复试验来弄清楚什么被拒绝了,什么没有被拒绝,直到我知道我所拥有的是正确的。