How to add a more complex validator schema to mongoDB using mongocxx

47 Views Asked by At

I'm looking for a way to apply validator schema to my MongoDB collections using the API from mongocxx but the documentation about the required syntax for more complex schemas is lacking.

Document Validation

Is there a more complete example somewhere?

Is there a way to read a json schema file and use it as a string to set a collection schema?

At the moment, I manually copy my collection schema generated from one of my serializing tool into MongoCompass but I need a way to apply the same schema from mongocxx.

Example json schema file :

{
  $jsonSchema: {
    properties: {
      someId: {
        type: 'number'
      },
      someVar: {
        type: 'number'
      },
      someOtherVar: {
        type: 'string'
      }
    }
  }
}

I tried using the option parameter in the create_collection function and also the run_command but both will fail with some typing error where it expects an object where a string was provided.

db.run_command(make_document(
   kvp("collMod", tableName),
   kvp("validator", schemaFile.c_str())
));

Any idea how to proceed if it is even possible? Thanks

0

There are 0 best solutions below