Search code examples
pythonjsonmongodbpymongo

Pymongo.db.create_collection with validator returns 'unknown operator: $jsonSchema'


I am trying to create a schema for my mongoDB database in order to ensure data format in the Database.

I am using pymongo to make sure this happens in my docker container when application starts, if the database is not initialized.

self.client = MongoClient(conn_string)
self.db = self.client[db_name]
# ...
# I am looping over in my json and 'stuff' is the collection name
# and json['stuff'] is the schema, so:
# collection = 'stuff'
# schema = json[collection]
# ... 
self.db.create_collection(collection, validator= { "validator": { "$jsonSchema": schema} })

The schema looks like that, and is loaded from a json file. (and that work)

{
   "stuff": {
      "bsonType": "object",
      "required": [ "sid", "name", "url" ],
      "properties": {
         "sid": {
            "bsonType": "string",
            "pattern": "^[a-z_]{3-20}$",
            "description": "must be a string 3-20 lowercase chars and is required"
         },
         "name": {
            "bsonType": "string",
            "minLength": 2,
            "maxLength": 30,
            "description": "must be a string 2-30 chars and is required"
         },
         "url": {
            // etc.
         }
      }
   }
}

But I get this error:

pymongo.errors.OperationFailure: unknown operator: $jsonSchema, full error: 
  {'ok': 0.0, 'errmsg': 'unknown operator: $jsonSchema', 'code': 2, 'codeName': 'BadValue'}

Solution

  • So I found the answer, also thanks to @prasad_ comment: The problem was a syntax error here are the bad and good version side to side :

    # Bad version :
    self.db.create_collection(collection, validator= { "validator": { "$jsonSchema": schema} })
    # Good version :
    self.db.create_collection(collection, validator= { "$jsonSchema": schema})
    

    So because the validator is passed as parameter, it shouldn't be named inside itself, only the $jsonSchema

    I hope it helps some people, it is hard to find proper documentation about pymongo usage, so let me know.