temporary_user_name
temporary_user_name

Reputation: 37018

How can I add a schema validation for requiring an array of strings in MongoDB from Mongo Shell?

I am intending to run the following command in Mongo Shell on my Collection once I have it perfected. However, I cannot figure out how to handle the last item in my required properties list, because it's the only one that is an array. It's an array of strings, to be specific. It's the last item, the imageIDs property. I put enum but I don't think that's right. How can I require its type to be an array of strings?

    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"
} )

Upvotes: 3

Views: 2982

Answers (1)

temporary_user_name
temporary_user_name

Reputation: 37018

For imageIDs, the property that needed to be an array of strings, I changed the schema with the following specifications:

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

Honestly the MongoDB documentation is kind of exhausting and severely lacking in examples. This page on the json schema properties is mildly helpful. I figured this out by just trial and error to see what was rejected and what wasn't until I knew that what I had was correct.

Upvotes: 9

Related Questions