mongodb-json-schema-4
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

MongoDB JSON Schema Draft 4 - $jsonSchema

A type definition of the JSON schema used in $jsonSchema operator in a mongodb collection validator.

https://www.mongodb.com/docs/manual/reference/operator/query/jsonSchema/

Create a collection with validation

https://www.mongodb.com/docs/v6.2/core/schema-validation/update-schema-validation/#steps

db.createCollection("users", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["username", "password"],
      properties: {
        username: {
          bsonType: "string",
          description: "must be a string and is required",
        },
        password: {
          bsonType: "string",
          minLength: 8,
          description: "must be a string at least 8 characters long, and is required",
        },
      },
    },
  },
});

Modify the validation schema

https://www.mongodb.com/docs/v6.2/core/schema-validation/update-schema-validation/#modify-the-validation-schema.

db.runCommand({
  collMod: "users",
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["username", "password"],
      properties: {
        username: {
          bsonType: "string",
          description: "must be a string and is required",
        },
        password: {
          bsonType: "string",
          minLength: 12,
          description: "must be a string of at least 12 characters, and is required",
        },
      },
    },
  },
});

Package Sidebar

Install

npm i mongodb-json-schema-4

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

15 kB

Total Files

7

Last publish

Collaborators

  • igrek8