Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/igrek8/mongodb-json-schema-4

MongoDB JSON Schema (draft-04) used in $jsonSchema validator
https://github.com/igrek8/mongodb-json-schema-4

jsonschema mongodb

Last synced: about 1 month ago
JSON representation

MongoDB JSON Schema (draft-04) used in $jsonSchema validator

Awesome Lists containing this project

README

        

# 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

```js
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.

```js
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",
},
},
},
},
});
```