0

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": "60300d5d756a0b91278f2a3d",
   "UserId": 2,
   "UserName": "UserName_1",
   "Password": "Password_1",
  "PasswordSalt": "InstallSchema",
  "CreatedDateUtc": "2021-02-19T19:11:22.6438703Z"
}

This is the latest of the many schemas that I have tried to use.

  {
  "bsonType": "object",
  "properties": {
    "_id": {
      "bsonType": "string"
    },
    "UserId": {
      "bsonType": "int"
    },
    "UserName": {
      "bsonType": "string"
    },
    "Password": {
      "bsonType": "string"
    },
    "PasswordSalt": {
      "bsonType": "string"
    },
    "CreatedDateUtc": {
      "bsonType": "date"
    }
  },
  "required": ["_id", "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, as an objectid 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.

2
  • This question doesn't really belong here because it has nothing to do with database administration. Having said that, doesn't declaring a field as a union of [ "string", "null" ] contradict declaring that field also as "required"? – mustaccio Feb 19 at 18:21
  • @mustaccio - Its a mongodb schema validation question, and schema management is an administration function. The schema was originally generated by another tool that put those unions in there, i had taken them out and it didnt make a difference. I'll update the question with the latest schema and document. – StingyJack Feb 19 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.