Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/igrek8/mongodb-json-schema-4
- Owner: igrek8
- Created: 2023-09-04T09:31:58.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-04T10:07:55.000Z (over 1 year ago)
- Last Synced: 2024-12-15T06:17:52.297Z (about 2 months ago)
- Topics: jsonschema, mongodb
- Language: TypeScript
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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",
},
},
},
},
});
```