I am trying to create a collection with JsonSchema (really its “BsonSchema”) validation. This is a document that I want to insert (with bogus values). Note that Id is coming from the mongo c# driver as the ObjectID via StringObjectIdGenerator.
{
"Id": "602ff3ed562ec58cb18c37c3",
"UserId": 2,
"UserName": "UserName_1",
"Password": "Password_1",
"PasswordSalt": "InstallSchema",
"CreatedDateUtc": "2021-02-19T17:22:50.86151Z"
}
This is one of the many schemas that I have tried to use.
{
"bsonType": "object",
"properties": {
"Id": {
"bsonType": "objectid"
},
"UserId": {
"bsonType": "int"
},
"UserName": {
"bsonType": ( "string", "null" )
},
"Password": {
"bsonType": ( "string", "null" )
},
"PasswordSalt": {
"bsonType": ( "string", "null" )
},
"CreatedDateUtc": {
"bsonType": "date"
}
},
"required": ( "UserId", "UserName", "Password", "PasswordSalt", "CreatedDateUtc" )
}
When inserting the document into the collection it reports “Document failed validation” with no more specific detail. I know the document is correct, its the schema that is not, but I cant figure out what is wrong with it. I’ve tried different variations of the Id
property including _id
, a string type, and removing it completely, but I dont know if thats even the problem. Any ideas on how to figure this out?
Also I did try creating the collection without schema validation and the document inserts fine. Then in Compass, I ran analysis for the collection and exported what it calls a schema (a very different format from the validator schema) and the properties and types match the schema above.