@@ -17,6 +17,13 @@ JSON Schema is a vocabulary that allows you to annotate and validate
17
17
JSON documents. You can use JSON schema to specify validation rules for
18
18
your fields in a human-readable format.
19
19
20
+ Compatibility
21
+ -------------
22
+
23
+ .. |operator-method| replace:: JSON schema validation
24
+
25
+ .. include:: /includes/fact-compatibility.rst
26
+
20
27
Context
21
28
-------
22
29
@@ -40,38 +47,20 @@ document.
40
47
41
48
.. procedure::
42
49
43
- .. step:: Create a collection with validation .
50
+ .. step:: Connect to your MongoDB deployment .
44
51
45
- Create a ``students`` collection and use the :query:`$jsonSchema`
46
- operator to set schema validation rules. For example:
52
+ To connect to a local MongoDB instance or
53
+ {+atlas+} deployment using :binary:`~bin.mongosh`,
54
+ refer to the steps in :mongosh:`Connect to a Deployment </connect>`
55
+ or :atlas:`Connect via mongosh </mongo-shell-connection/>`.
47
56
48
- .. code-block :: javascript
57
+ .. step :: Create a collection with validation.
49
58
50
- db.createCollection("students", {
51
- validator: {
52
- $jsonSchema: {
53
- bsonType: "object",
54
- title: "Student Object Validation",
55
- required: [ "address", "major", "name", "year" ],
56
- properties: {
57
- name: {
58
- bsonType: "string",
59
- description: "'name' must be a string and is required"
60
- },
61
- year: {
62
- bsonType: "int",
63
- minimum: 2017,
64
- maximum: 3017,
65
- description: "'year' must be an integer in [ 2017, 3017 ] and is required"
66
- },
67
- gpa: {
68
- bsonType: [ "double" ],
69
- description: "'gpa' must be a double if the field exists"
70
- }
71
- }
72
- }
73
- }
74
- } )
59
+ In :binary:`~bin.mongosh`, run the following command to
60
+ create a ``students`` collection and use the
61
+ :query:`$jsonSchema` operator to set schema validation rules:
62
+
63
+ .. include:: /includes/schema-validation/json-schema-example.rst
75
64
76
65
.. tip:: Clarify Rules with Title and Description Fields
77
66
@@ -82,61 +71,66 @@ document.
82
71
83
72
.. step:: Confirm that the validation prevents invalid documents.
84
73
85
- The following insert operation fails because ``gpa`` is an integer
86
- when the ``validator`` requires a ``double``.
87
-
88
- .. code-block:: javascript
89
- :emphasize-lines: 5
90
-
91
- db.students.insertOne( {
92
- name: "Alice",
93
- year: Int32( 2019 ),
94
- major: "History",
95
- gpa: Int32(3),
96
- address: {
97
- city: "NYC",
98
- street: "33rd Street"
99
- }
100
- } )
101
-
102
- The operation returns this error:
103
-
104
- .. code-block:: javascript
105
- :copyable: false
106
-
107
- MongoServerError: Document failed validation
108
-
109
- Additional information: {
110
- failingDocumentId: ObjectId("630d093a931191850b40d0a9"),
111
- details: {
112
- operatorName: '$jsonSchema',
113
- title: 'Student Object Validation',
114
- schemaRulesNotSatisfied: [
115
- {
116
- operatorName: 'properties',
117
- propertiesNotSatisfied: [
118
- {
119
- propertyName: 'gpa',
120
- description: "'gpa' must be a double if the field exists",
121
- details: [
122
- {
123
- operatorName: 'bsonType',
124
- specifiedAs: { bsonType: [ 'double' ] },
125
- reason: 'type did not match',
126
- consideredValue: 3,
127
- consideredType: 'int'
128
- }
129
- ]
130
- }
131
- ]
74
+ Run the following command. The insert operation fails
75
+ because ``gpa`` is an integer when the ``validator`` requires a
76
+ ``double``.
77
+
78
+ .. io-code-block::
79
+ :copyable: true
80
+
81
+ .. input::
82
+ :language: javascript
83
+ :emphasize-lines: 5
84
+
85
+ db.students.insertOne( {
86
+ name: "Alice",
87
+ year: Int32( 2019 ),
88
+ major: "History",
89
+ gpa: Int32(3),
90
+ address: {
91
+ city: "NYC",
92
+ street: "33rd Street"
132
93
}
133
- ]
134
- }
135
- }
94
+ } )
95
+
96
+ .. output::
97
+ :language: json
98
+
99
+ MongoServerError: Document failed validation
100
+
101
+ Additional information: {
102
+ failingDocumentId: ObjectId("630d093a931191850b40d0a9"),
103
+ details: {
104
+ operatorName: '$jsonSchema',
105
+ title: 'Student Object Validation',
106
+ schemaRulesNotSatisfied: [
107
+ {
108
+ operatorName: 'properties',
109
+ propertiesNotSatisfied: [
110
+ {
111
+ propertyName: 'gpa',
112
+ description: "'gpa' must be a double if the field exists",
113
+ details: [
114
+ {
115
+ operatorName: 'bsonType',
116
+ specifiedAs: { bsonType: [ 'double' ] },
117
+ reason: 'type did not match',
118
+ consideredValue: 3,
119
+ consideredType: 'int'
120
+ }
121
+ ]
122
+ }
123
+ ]
124
+ }
125
+ ]
126
+ }
127
+ }
136
128
137
129
.. step:: Insert a valid document.
138
130
139
- The insert succeeds after you change the ``gpa`` field to a double:
131
+ If you change the ``gpa`` field value to a ``double`` type, the
132
+ insert operation succeeds. Run the following command to
133
+ insert the valid document:
140
134
141
135
.. code-block:: javascript
142
136
:emphasize-lines: 5
@@ -154,28 +148,36 @@ document.
154
148
155
149
.. step:: Query for the valid document.
156
150
157
- To confirm that the document was successfully inserted, query the
158
- ``students`` collection:
151
+ To confirm that you've successfully inserted the document, run
152
+ the following command to query the ``students`` collection:
159
153
160
- .. code-block:: javascript
154
+ .. io-code-block::
155
+ :copyable: true
161
156
162
- db.students.find()
157
+ .. input::
158
+ :language: javascript
163
159
164
- MongoDB returns the inserted document:
160
+ db.students.find()
165
161
166
- .. code-block:: javascript
167
- :copyable: false
168
-
169
- [
170
- {
171
- _id: ObjectId("62bb413014b92d148400f7a5"),
172
- name: 'Alice',
173
- year: 2019,
174
- major: 'History',
175
- gpa: 3,
176
- address: { city: 'NYC', street: '33rd Street' }
177
- }
178
- ]
162
+ .. output::
163
+ :language: json
164
+
165
+ [
166
+ {
167
+ _id: ObjectId("62bb413014b92d148400f7a5"),
168
+ name: 'Alice',
169
+ year: 2019,
170
+ major: 'History',
171
+ gpa: 3,
172
+ address: { city: 'NYC', street: '33rd Street' }
173
+ }
174
+ ]
175
+
176
+ .. tip::
177
+
178
+ If you're connected to an Atlas deployment, you can also
179
+ :atlas:`view and filter for the document in the Atlas UI
180
+ </atlas-ui/documents/#view--filter--and-sort-documents>`.
179
181
180
182
Additional Information
181
183
----------------------
0 commit comments