0

Executing Schema Validation setup script is failing for a MongoDB collection. Unable to configure the schema validator.

I am trying to add a Schema Validation to a MongoDB collection and facing the following error.

{
    "message" : "Document validators are not allowed on collection local.product with UUID b12596d5-3bc5-4321-94ef-ac006fb32abb in the local internal database",
    "ok" : 0,
    "code" : 72,
    "codeName" : "InvalidOptions"
}

This is the validator script that I am trying to execute, am I missing something here?

var validator={
    $jsonSchema : {
        bsonType : "object",
        additionalProperties : true,
        required : [ "productName" ],
        properties : {
            productName : {
                bsonType : "string",
                description : "must be a string and is required"
            }
        }
    }
};

db.runCommand( {
  collMod: "product",
  validator,
  validationLevel: "strict"
})
CSK 4ever
  • 45
  • 4
  • Using the `local` database is playing with fire. It is a system database that [_"... stores data used in the replication process, and other instance-specific data."_](https://www.mongodb.com/docs/manual/reference/local-database/). – rickhg12hs May 23 '23 at 18:45
  • @rickhg12hs - but this is my local mongod instance, I am not trying to use any remote mongod local to add this schema validation. – CSK 4ever May 23 '23 at 19:55
  • If you create a database, e.g., `store`, and then try your `validator`, does it work then? – rickhg12hs May 23 '23 at 21:37
  • 1
    @rickhg12hs - yes creating a separate DB works instead of local. This helps, thank you. I missed this instruction earlier, https://www.mongodb.com/docs/v6.0/core/schema-validation/specify-json-schema/#restrictions – CSK 4ever May 23 '23 at 23:25

0 Answers0