@@ -51,16 +51,16 @@ To specify JSON Schema validation, use the
5151:manual:`$jsonSchema </reference/operator/query/jsonSchema>`
5252operator.
5353
54- .. code-block:: json
54+ .. code-block:: javascript
5555
5656 {
5757 $jsonSchema: {
58- required: ['customer' ], // the customer field is required
58+ required: ['name', 'borough' ], // the name and borough fields are required
5959 properties: {
60- purchaseMethod : {
61- enum: ['In Store','Online'] ,
62- description: "can only be either 'In Store' or 'Online'"
63- }
60+ cuisine : {
61+ bsonType: "string" ,
62+ description: "must be a string"
63+ }
6464 }
6565 }
6666 }
@@ -72,61 +72,68 @@ example:
7272- The ``required`` array defines required fields in your document.
7373
7474- The ``properties`` object defines rules for specific document
75- fields.
75+ fields.
7676
7777Consider the following example validation:
7878
79- .. code-block:: json
79+ .. code-block:: javascript
8080
8181 {
8282 $jsonSchema: {
8383 bsonType: "object",
84- required: [ "name ", "year ", "major", "gpa", "address.city", "address.street " ],
84+ required: [ "address ", "borough ", "name " ],
8585 properties: {
86- name: {
87- bsonType: "string",
88- description: "must be a string"
86+ address: {
87+ bsonType: "object",
88+ properties: {
89+ coord: {
90+ bsonType: "array",
91+ items: [
92+ {
93+ bsonType: "double",
94+ minimum: -180,
95+ maximum: 180,
96+ exclusiveMaximum: false,
97+ description: "must be a number in [ -180, 180 ]"
98+ },
99+ {
100+ bsonType: "double",
101+ minimum: -90,
102+ maximum: 90,
103+ exclusiveMaximum: false,
104+ description: "must be a number in [ -90, 90 ]"
105+ }
106+ ]
107+ }
108+ },
109+ description: "must be an object"
89110 },
90- year: {
91- bsonType: "int",
92- minimum: 2017,
93- maximum: 3017,
94- exclusiveMaximum: false,
95- description: "must be an integer in [ 2017, 3017 ]"
96- },
97- major: {
111+ borough: {
98112 bsonType: "string",
99- enum: [ "Math", "English", "Computer Science", "History", null ],
100- description: "can only be one of the enum values"
101- },
102- gpa: {
103- bsonType: [ "double" ],
104- minimum: 0,
105- description: "must be a double"
113+ enum: [ "Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island" ],
114+ description: "must be one of the enum strings"
106115 }
107116 }
108117 }
109118 }
110119
120+
111121This validation specifies:
112122
113123- The list of
114124 :manual:`required </reference/operator/query/jsonSchema/#available-keywords>`
115125 fields.
116126
117127- The :manual:`bsonType </reference/operator/query/jsonSchema/#available-keywords>` for
118- all fields.
128+ all required fields.
119129
120130- The :manual:`minimum </reference/operator/query/jsonSchema/#available-keywords>`
121131 and :manual:`maximum </reference/operator/query/jsonSchema/#available-keywords>`
122- values for the ``year `` field .
132+ values in the ``address.coord `` array .
123133
124- - The acceptable values for the ``major `` field, using
134+ - The acceptable values for the ``borough `` field, using
125135 :manual:`enum </reference/operator/query/jsonSchema/#available-keywords>`.
126136
127- - The :manual:`minimum </reference/operator/query/jsonSchema/#available-keywords>`
128- value for the ``gpa`` field.
129-
130137For all available ``$jsonSchema`` keywords, refer to the
131138:manual:`$jsonSchema </reference/operator/query/jsonSchema>` page in
132139the MongoDB manual.
@@ -139,25 +146,24 @@ You can also specify validation using
139146exception of the following query operators: :query:`$near`,
140147:query:`$nearSphere`, :query:`$text`, and :query:`$where`.
141148
142- .. code-block:: json
149+ .. code-block:: javascript
143150
144151 {
145152 $or: [
146- { phone: { $type: "string" } },
147- { email: { $regex: /@mongodb\.com$/ } },
148- { status: { $in: [ "Unknown", "Incomplete" ] } }
153+ { name: { $type: "string" } },
154+ { borough: {
155+ bsonType: "string",
156+ enum: [ "Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island" ],
157+ description: "must be one of the enum strings"
158+ } }
149159 ]
150160 }
151161
152162Using this validation, *one* of the following must be true:
153163
154- - The ``phone`` field must be BSON type string,
155-
156- - The ``email`` field must match the
157- :manual:`regex </reference/operator/query/regex/>`
158- ``/@mongodb\.com$/``, or
164+ - The ``name`` field must be BSON type string.
165+ - The ``borough`` field must be one of the enum strings.
159166
160- - The ``status`` field must be either ``Unknown`` or ``Incomplete``.
161167
162168.. _validation-actions-levels:
163169
0 commit comments