question

DakaraiMcCoy-2805 avatar image
0 Votes"
DakaraiMcCoy-2805 asked NavtejSaini-MSFT answered

How to set collection schema for CosmosDB MongoDB API 3.6 using Pymongo

I have been searching for a way to instantiate a collection with a schema using the Pymongo API to access the Cosmos DB API.
I see the MongoDB API 3.6 does not support the 'myColl' database operator. Is there another method to do this? I need a method that I can script so that the process can be automated and run in the background.

azure-cosmos-db
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DakaraiMcCoy-2805 We are checking this and will get back to you.

1 Vote 1 ·

@DakaraiMcCoy-2805

Here are some of the documents - Document1, Document2 external to Microsoft which you can refer for getting more info. If you still need further help, please get back to us.

Regards
Navtej S


1 Vote 1 ·

Hi @NavtejSaini-MSFT,

Thank you for following up. After looking at the documents I need to clarify my question. The sources you provided do illustrate how to connect to the CosmosDB and create a collection, however none of them demonstrate how to create a collection and insert a specific schema for validation. Currently anything is accepted as true even when I give data that does not match the schema.

best,

Dakarai

0 Votes 0 ·

I've tried following this option here however I get the error that 'collmod' is not supported. I see the query operator $jsonschema is not supported as well in the documentation. I've tried an approach with the pymongo.collection.Collection class however even if it accepts the validator parameter, anything is still acceptable in the collection.

0 Votes 0 ·

here is an example script.




 import datetime
 import pymongo
 validSchema = {'name': [{'bysontype': 'string'}], 'date': [{'bsontype': 'Date'}]}
 gooddata = {'name': 'hello', 'date': datetime.datetime.fromisoformat('2021-02-16 11:59:00')}
 baddata = {'x': 1}
 client = pymongo.MongoClient(conection_uri)
 temp = pymongo.collection.Collection(client['_socket_for_writes'], 'temp', validator = validSchema)
 good = temp.insert_one(gooddata)
 good.acknowledged
 bad = temp.insert_one(baddata)
 bad.acknowledged

with the output


 >>> good.acknowledged
 True
 >>> bad.acknowledged
 True
 >>> temp.estimated_document_count()
 2








0 Votes 0 ·

We are checking this with our team.

0 Votes 0 ·

1 Answer

NavtejSaini-MSFT avatar image
0 Votes"
NavtejSaini-MSFT answered

@DakaraiMcCoy-2805

We got info from our team - Cosmos DB doesn’t support schema validation, but here’s a workaround in the last answer of this thread.

Hope this helps.

Regards
Navtej S


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @NavtejSaini-MSFT ,

Thank you for taking the time to get back to me.
I have found this page before reaching out to you and tried the solution you suggest.
However I get an error after the db.command(cmd) that says collMod is not an accepted parameter and the documentation verifies this command is not supported.

 File "C:\***\lib\site-packages\pymongo\helpers.py", line 160, in _check_command_response
     raise OperationFailure(errmsg, code, response, max_wire_version)
 pymongo.errors.OperationFailure: Command collMod not supported., full error: {'ok': 0.0, 'errmsg': 'Command collMod not supported.', 'code': 115, 'codeName': 'CommandNotSupported'}

0 Votes 0 ·

I tried deleting the collmod operator and re-running my script and this is the error I receive.

 File "C:\***\lib\site-packages\pymongo\helpers.py", line 160, in _check_command_response
     raise OperationFailure(errmsg, code, response, max_wire_version)
 pymongo.errors.OperationFailure: Command validator not found., full error: {'ok': 0.0, 'errmsg': 'Command validator not found.', 'code': 59, 'codeName': 'CommandNotFound'}


I also tried deleting the validator operator and this is the error.

 File "C:\***\lib\site-packages\pymongo\helpers.py", line 160, in _check_command_response
     raise OperationFailure(errmsg, code, response, max_wire_version)
 pymongo.errors.OperationFailure: Command $jsonSchema not found., full error: {'ok': 0.0, 'errmsg': 'Command $jsonSchema not found.', 'code': 59, 'codeName': 'CommandNotFound'}





0 Votes 0 ·

After deleting the $jsonschema operator received another error.

 File "C:\***\lib\site-packages\pymongo\helpers.py", line 160, in _check_command_response
     raise OperationFailure(errmsg, code, response, max_wire_version)
 pymongo.errors.OperationFailure: Command bsontype not found., full error: {'ok': 0.0, 'errmsg': 'Command bsontype not found.', 'code': 59, 'codeName': 'CommandNotFound'}

It seems without the $jsonschema operator there is no way to use the json-style syntax for validator declaration, so operators like bsontype will not be recognized.
Looking at the documentation (see Other Query Operators) there seems to be another approach however, I've also pursued path with no luck as well.


0 Votes 0 ·

@DakaraiMcCoy-2805

At this point our team has conveyed to reach out to Mongo DB forums for checking on how to do this without Schema validation.

Also you can reach out to our support team for further help. Please raise ticket and share the same with us so that we can get traction for the same.

Regards
Navtej S

0 Votes 0 ·