Defining data schema using pymongo

Hello everyone,

Is there any example to create $jsonschema validator in pymongo library(in python)?

db.createCollection("collect", {
  validator: {
        $jsonSchema: {
            bsonType: "object",
            additionalProperties: true,
            required: ["component", "path"],
            properties: {
                component: {
                    bsonType: "string"
                path: {
                   bsonType: "string",
                   description: "Set to default value"

i require to implement in pymongo instead of js.

Best Regards,
M Jagadeesh

Here is a code sample using pymongo to create a new collection with a jsonschema:

from pymongo import MongoClient

def create_collection(coll_name):
    client = MongoClient('mongodb://localhost:27017/')
    db = client.test
    result = db.create_collection(coll_name, validator={
        '$jsonSchema': {
            'bsonType': 'object',
            'additionalProperties': True,
            'required': ['component', 'path'],
            'properties': {
                'component': {
                    'bsonType': 'string'
                'path': {
                    'bsonType': 'string',
                    'description': 'Set to default value'


if __name__ == '__main__':

As you can see in this screenshot, I got the correct jsonschema in my MDB server:

1 Like

Thank you for example in pymongo!! @MaBeuLux88.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.