Extract MongoDB Schema JSON from Schema output

I am trying to find a way to add Schema Validation to my MongoDB collections. I am using 5.0.14 and it appears that you can add in a JSON schema (with a subset of the validation format in JSON 4) as the format for the documents. Great. What I’m trying to accomplish is to sample a subset of the documents (I can do this via MongoDB Compass by going to a collection, Click on the Collections dropdown at the top and choose Share Schema as JSON. An example of what that looks like is included here:

{
  "count": 79,
  "fields": [
    {
      "name": "_id",
      "path": [
        "_id"
      ],
      "count": 79,
      "type": "String",
      "probability": 1,
      "hasDuplicates": false,
      "types": [
        {
          "name": "String",
          "path": [
            "_id"
          ],
          "count": 79,
          "probability": 1,
          "unique": 79,
          "hasDuplicates": false,
          "values": [
            "ExValue1",
            "ExValue2",
...

What I’d like to do is use this JSON Schema export as the definition for Schema Validation that you can insert via the Validation tab in the same collection view. An example of what that looks like is included here from the documentation:

db.createCollection("students", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         title: "Student Object Validation",
         required: [ "address", "major", "name", "year" ],
         properties: {
            name: {
               bsonType: "string",
               description: "'name' must be a string and is required"
            },
            year: {
               bsonType: "int",
               minimum: 2017,
               maximum: 3017,
               description: "'year' must be an integer in [ 2017, 3017 ] and is required"
            },
            gpa: {
               bsonType: [ "double" ],
               description: "'gpa' must be a double if the field exists"
            }
         }
      }
   }
} )

Now this looks nothing like the output from the tool itself and before I go spend time writing a tool to convert the Schema output format into the the validation format, I can’t help but think that this is a widely used tool, with lots of support and a massive user base, that somebody has solved this problem already and knows how to do it. Would anyone lend some help?

Hi @William_Jones - while we don’t have an official solution right now, I wanted to let you know that we are planning to build the feature you requested in Compass. If you have any specific feedback or requirements for the experience, please feel free to DM me!