Skip to content

Commit 6142de6

Browse files
davidhou17mdb-ashleysarahsimpers
authored
(DOCSP-32321): Add Atlas-centric content to JSON Schema Validation page (#4765)
* Add Atlas compatability and step * Apply Ashley's suggestion Co-authored-by: Ashley Brown <[email protected]> * Apply Sarah's suggestion Co-authored-by: Sarah Simpers <[email protected]> --------- Co-authored-by: Ashley Brown <[email protected]> Co-authored-by: Sarah Simpers <[email protected]>
1 parent 8a85829 commit 6142de6

File tree

2 files changed

+127
-98
lines changed

2 files changed

+127
-98
lines changed

source/core/schema-validation/specify-json-schema.txt

Lines changed: 100 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ JSON Schema is a vocabulary that allows you to annotate and validate
1717
JSON documents. You can use JSON schema to specify validation rules for
1818
your fields in a human-readable format.
1919

20+
Compatibility
21+
-------------
22+
23+
.. |operator-method| replace:: JSON schema validation
24+
25+
.. include:: /includes/fact-compatibility.rst
26+
2027
Context
2128
-------
2229

@@ -40,38 +47,20 @@ document.
4047

4148
.. procedure::
4249

43-
.. step:: Create a collection with validation.
50+
.. step:: Connect to your MongoDB deployment.
4451

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/>`.
4756

48-
.. code-block:: javascript
57+
.. step:: Create a collection with validation.
4958

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
7564

7665
.. tip:: Clarify Rules with Title and Description Fields
7766

@@ -82,61 +71,66 @@ document.
8271

8372
.. step:: Confirm that the validation prevents invalid documents.
8473

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"
13293
}
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+
}
136128

137129
.. step:: Insert a valid document.
138130

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:
140134

141135
.. code-block:: javascript
142136
:emphasize-lines: 5
@@ -154,28 +148,36 @@ document.
154148

155149
.. step:: Query for the valid document.
156150

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:
159153

160-
.. code-block:: javascript
154+
.. io-code-block::
155+
:copyable: true
161156

162-
db.students.find()
157+
.. input::
158+
:language: javascript
163159

164-
MongoDB returns the inserted document:
160+
db.students.find()
165161

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

180182
Additional Information
181183
----------------------
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.. code-block:: javascript
2+
3+
db.createCollection("students", {
4+
validator: {
5+
$jsonSchema: {
6+
bsonType: "object",
7+
title: "Student Object Validation",
8+
required: [ "address", "major", "name", "year" ],
9+
properties: {
10+
name: {
11+
bsonType: "string",
12+
description: "'name' must be a string and is required"
13+
},
14+
year: {
15+
bsonType: "int",
16+
minimum: 2017,
17+
maximum: 3017,
18+
description: "'year' must be an integer in [ 2017, 3017 ] and is required"
19+
},
20+
gpa: {
21+
bsonType: [ "double" ],
22+
description: "'gpa' must be a double if the field exists"
23+
}
24+
}
25+
}
26+
}
27+
} )

0 commit comments

Comments
 (0)