Validation error in MongoDB, but validating at jsonschema.dev?

Hey,
I’ve been restarting my knowledge in MonogDB. As part of chapter 3 I decided to explore the document schema sections to learn more about it.

I decided to try out the example given in under the title JSON Schema given here

db.createCollection("students", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: [ "name", "year", "major", "address" ],
         properties: {
            name: {
               bsonType: "string",
               description: "must be a string and is required"
            },
            year: {
               bsonType: "int",
               minimum: 2017,
               maximum: 3017,
               description: "must be an integer in [ 2017, 3017 ] and is required"
            },
            major: {
               enum: [ "Math", "English", "Computer Science", "History", null ],
               description: "can only be one of the enum values and is required"
            },
            gpa: {
               bsonType: [ "double" ],
               description: "must be a double if the field exists"
            },
            address: {
               bsonType: "object",
               required: [ "city" ],
               properties: {
                  street: {
                     bsonType: "string",
                     description: "must be a string if the field exists"
                  },
                  city: {
                     bsonType: "string",
                     description: "must be a string and is required"
                  }
               }
            }
         }
      }
   }
})

Then i created my own document to insert.

{
	"name": "Abrisham", 
	"year": 2022, 
	"major": null,
	"gpa": null,
	"address" : {
		"street" : null,
		"city" : "Lismore"
		}
	}

I read around and found this tool for JSON document validation at jsonschema

Hopefully you can see that my document has been confirmed as valid by this program, however when I try insert it into the newly created collection ‘students’ a validation error is given.

I was wanting hear from others about what they think the reasons for this could be because I’ve been trying for an hour and considering the validation confirmation by JSON Schema Validation it’s made me want to have another set of eyes look at this problem.

Please suggest why the document would not be validating.

I am not sure but most likely

fails the following

I think you either have to leave gpa:null out of your document or add null as a valid bsonType for gpa in your schema. I also think that having address.street set to null is not allowed by your schema.

2 Likes

Thank you,

I tried putting in values and it worked.

There are three options that can be used to resolve this, my reference is this stack exchange discussion: Storing null vs not storing the key at all in MongoDB - Stack Overflow

  1. Adding bsonType null as valid for both street and gpa.
  2. If the field value is not required and has not been provided then it does not need to be included
  3. Adding “” as the field value.

A user on stack exchange with a reputable reputation recommends against using “” as the field value, as the value will be type string rather than null or empty causing a conflict because isKeyExists(key) will return True when it is false.

Thank you Steeve for your reply it was very helpful in leading me towards further learning.