$jsonSchema

noun

say it: “DOL-er JAY-sahn SKEE-muh” /ˈdɑlər ˈdʒeɪsɑn ˈskimə/

A MongoDB operator that describes, in JSON Schema form, which fields a document must have and what each may hold.

What is $jsonSchema in MongoDB?

$jsonSchema describes the shape a collection's documents must have: the required fields, and each field's bsonType, minimum or allowed values (enum). Used as a validator, it makes MongoDB refuse a document that doesn't fit.

db.createCollection("products", { validator: { $jsonSchema: {
  required: ["name", "price"],
  properties: { price: { bsonType: "number", minimum: 0 } }
} } });
db.products.insertOne({ name: "Bell" });
// MongoServerError: Document failed validation

In a sentence

  • “In $jsonSchema, enum lists the only values a field may hold, such as bike, helmet and light.”
  • “The $jsonSchema validator refused a product with no price.”

Level: Journeyman · More MongoDB terms at this level

Updated