mongodbvalidationschemapymongowrite-error

MongoDb schema validation error when schema specified ['string'] as bson type of attribute


I have a peice of python code which is using the PyMongo library to insert into my mongodb Collection hosted on MongoDB Atlas.

Im trying to insert the following doc into a collection called Customer deployed on Atlas using an insert_many() call.

{'Source': ['LoanDelivery_2022-03-31'], 'CustID': 1, 'CustName': 'Seexxx', 'CustPhone': '940xxxxx', '_id': ObjectId('66ca8d5873a3fdxxxx')}

The schema for the collection is as follows.

{
  $jsonSchema: {
    title: 'Customers_Schema',
    properties: {
      CustID: {
        bsonType: 'int'
      },
      CustName: {
        bsonType: 'string'
      },
      CustPhone: {
        bsonType: 'string'
      },
      CustAddress: {
        bsonType: 'string'
      },
      Source: {
        bsonType: [
          'string'
        ],
        description: 'Sources of the record'
      }
    },
    required: [
      'CustID',
      'CustName',
      'Source'
    ]
  }
}

I get an error saying considered type of Source is an array while expected type is ['string']. Aren't these two types equivalent, and Pymongo should be able to handle the binary conversion for me automatically? Is there any type conversion i need to handle within my python code before I write to MongoDB?

Heres a snippet of the BulkWriteError object thrown by the write operation

{'operatorName': 'bsonType', 'specifiedAs': {'bsonType': [...]}, 'reason': 'type did not match', 'consideredValue': ['LoanDelivery_2022-03-31'], 'consideredType': 'array'}
special variables
function variables
'operatorName' =
'bsonType'
'specifiedAs' =
{'bsonType': ['string']}
'reason' =
'type did not match'
'consideredValue' =
['LoanDelivery_2022-03-31']
'consideredType' =
'array'

Solution

  • The problem is that in the scheme description, the type can be specified as an array. i.e. bsonType: 'string' and bsonType: ['string'] are the same.

    The corrected schema is following:

       {
        $jsonSchema: {
        title: 'Customers_Schema',
        properties: {
          CustID: {
            bsonType: 'int'
          },
          CustName: {
            bsonType: 'string'
          },
          CustPhone: {
            bsonType: 'string'
          },
          CustAddress: {
            bsonType: 'string'
          },
          Source: {
            bsonType: [
              'array'
            ],
            description: 'Sources of the record',
            minItems: 1,
            items: {
               bsonType: ['string']
            }
          }
        },
        required: [
          'CustID',
          'CustName',
          'Source'
        ]
      }
    }