Challenge 3: Write a $jsonSchema Validator — Possible Solution ==================================================================== db.createCollection("products", { validator: { $jsonSchema: { bsonType: "object", required: ["name", "price"], properties: { name: { bsonType: "string" }, price: { bsonType: ["double", "decimal"], minimum: 0 } } } } }) WHY THIS WORKS AS AN ANSWER ------------------------------ Each requirement from the challenge maps onto a specific piece of the validator, following the exact structure from this chapter's example: - required: ["name", "price"] ensures both fields must be present on every inserted/updated document — an insert missing either one is now rejected outright, restoring the enforcement a MySQL NOT NULL constraint would have provided by default. - name: { bsonType: "string" } constrains the name field's type. - price: { bsonType: [...], minimum: 0 } constrains price to be numeric AND non-negative — listing both "double" and "decimal" as acceptable bsonTypes is a reasonable real-world choice here, since per this chapter's own money guidance, a price field might legitimately be stored as Decimal128 rather than Double, and the validator shouldn't reject a well-considered Decimal128 price just because it happens not to be the default Double type. This is the practical payoff of $jsonSchema: a collection that was previously happy to accept any shape of document at all now enforces the same two structural guarantees (required fields, correct types) that a relational table's column definitions would have provided automatically.