@@ -144,21 +144,20 @@ query expression:
144
144
145
145
:ref:`query operators <query-selectors>`
146
146
147
- Behavior
148
- --------
147
+ Behavior and Examples
148
+ ---------------------
149
149
150
150
Validation occurs during updates and inserts. When you add validation to
151
151
a collection, existing documents do not undergo validation checks until
152
152
modification.
153
153
154
+ Validation on Existing Documents
155
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156
+
154
157
To perform validation checks on existing documents, use the
155
158
:dbcommand:`validate` command or the :method:`db.collection.validate()`
156
159
shell helper.
157
160
158
-
159
- Existing Documents
160
- ~~~~~~~~~~~~~~~~~~
161
-
162
161
The ``validationLevel`` option determines which operations MongoDB
163
162
applies the validation rules:
164
163
@@ -176,7 +175,7 @@ documents:
176
175
177
176
.. code-block:: json
178
177
179
- db.contacts.insert ([
178
+ db.contacts.insertMany ([
180
179
{ "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" },
181
180
{ "_id": 2, "name": "Ivan", "city": "Vancouver" }
182
181
])
@@ -194,11 +193,11 @@ collection:
194
193
properties: {
195
194
phone: {
196
195
bsonType: "string",
197
- description: "must be a string and is required"
196
+ description: "phone must be a string and is required"
198
197
},
199
198
name: {
200
199
bsonType: "string",
201
- description: "must be a string and is required"
200
+ description: "name must be a string and is required"
202
201
}
203
202
}
204
203
} },
@@ -230,12 +229,12 @@ validation rule we created above that requires ``name`` to be a string.
230
229
231
230
.. code-block:: javascript
232
231
233
- db.contacts.update (
232
+ db.contacts.updateOne (
234
233
{ _id: 1 },
235
234
{ $set: { name: 10 } }
236
235
)
237
236
238
- db.contacts.update (
237
+ db.contacts.updateOne (
239
238
{ _id: 2 },
240
239
{ $set: { name: 20 } }
241
240
)
@@ -247,48 +246,44 @@ document did not meet the initial criteria when validation was added.
247
246
248
247
.. code-block:: javascript
249
248
:copyable: false
250
- :emphasize-lines: 9-27
251
249
252
250
// _id: 1
253
- WriteResult({
254
- "nMatched" : 0,
255
- "nUpserted" : 0,
256
- "nModified" : 0,
257
- "writeError" : {
258
- "code" : 121,
259
- "errmsg" : "Document failed validation",
260
- "errInfo" : {
261
- "failingDocumentId" : 1,
262
- "details" : {
263
- "operatorName" : "$jsonSchema",
264
- "schemaRulesNotSatisfied" : [
251
+ MongoServerError: Document failed validation
252
+ Additional information: {
253
+ failingDocumentId: 1,
254
+ details: {
255
+ operatorName: '$jsonSchema',
256
+ schemaRulesNotSatisfied: [
257
+ {
258
+ operatorName: 'properties',
259
+ propertiesNotSatisfied: [
265
260
{
266
- "operatorName" : "properties",
267
- "propertiesNotSatisfied" : [
268
- {
269
- "propertyName" : "name",
270
- "details" : [
271
- {
272
- "operatorName" : "bsonType",
273
- "specifiedAs" : {
274
- "bsonType" : "string"
275
- },
276
- "reason" : "type did not match",
277
- "consideredValue" : 10,
278
- "consideredType" : "double"
279
- }
280
- ]
281
- }
282
- ]
283
- }
284
- ]
261
+ propertyName: 'name',
262
+ description: 'name must be a string and is required',
263
+ details: [
264
+ {
265
+ operatorName: 'bsonType',
266
+ specifiedAs: { bsonType: 'string' },
267
+ reason: 'type did not match',
268
+ consideredValue: 10,
269
+ consideredType: 'int'
270
+ }
271
+ ]
272
+ }
273
+ ]
285
274
}
286
- }
287
- }
288
- })
275
+ ]
276
+ }
277
+ }
289
278
290
279
// _id: 2
291
- WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
280
+ {
281
+ acknowledged: true,
282
+ insertedId: null,
283
+ matchedCount: 1,
284
+ modifiedCount: 0,
285
+ upsertedCount: 0
286
+ }
292
287
293
288
To disable validation entirely, you can set ``validationLevel`` to
294
289
``off``.
@@ -333,16 +328,13 @@ Schema validator:
333
328
validationAction: "warn"
334
329
} )
335
330
336
- With the ``warn`` :collflag:`validationAction`, MongoDB logs any
337
- violations but allows the insertion or update to proceed.
338
-
339
331
For example, the following insert operation violates the validation rule:
340
332
341
333
.. code-block:: javascript
342
334
343
335
db.contacts2.insert( { name: "Amanda", status: "Updated" } )
344
336
345
- However, since the ``validationAction`` is ``warn`` only , MongoDB only
337
+ However, since the ``validationAction`` is ``warn``, MongoDB only
346
338
logs the validation violation message and allows the operation to
347
339
proceed. Run the following command to view the MongoDB logs:
348
340
@@ -384,6 +376,83 @@ readability) contains information like this:
384
376
\"specifiedAs\":{\"required\":[\"phone\"]},
385
377
\"missingProperties\":[\"phone\"]}]}}}}"
386
378
379
+ .. _validation-description-example:
380
+
381
+ Use Title and Description Fields to Clarify Validation Rules
382
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
383
+
384
+ .. include:: /includes/fact-5.1-schema-validation-description-overview.rst
385
+
386
+ Consider a ``users`` collection with the following schema validation:
387
+
388
+ .. code-block:: javascript
389
+
390
+ db.runCommand( {
391
+ collMod: "users",
392
+ validator: { $jsonSchema: {
393
+ bsonType: "object",
394
+ title: "Email validation",
395
+ properties: {
396
+ email: {
397
+ "bsonType": "string",
398
+ "pattern": "^@mongodb\.com$",
399
+ "description": "Email address must end with '@mongodb.com'"
400
+ },
401
+ }
402
+ } },
403
+ validationLevel: "moderate"
404
+ } )
405
+
406
+ The ``pattern`` field indicates that all ``email`` fields must
407
+ end with ``@mongodb.com``. If you try to insert a document that
408
+ does not match the pattern, MongoDB includes the validation
409
+ ``title`` and ``description`` in the error output:
410
+
411
+ Input:
412
+
413
+ .. code-block:: javascript
414
+
415
+ db.users.insertOne( { "name": "Amelia Morrison", "email": "
[email protected] " } )
416
+
417
+ Output:
418
+
419
+ .. code-block:: javascript
420
+ :copyable: false
421
+ :emphasize-lines: 6,13
422
+
423
+ MongoServerError: Document failed validation
424
+ Additional information: {
425
+ failingDocumentId: ObjectId("614a10bab93bbd15dd2e2eb6"),
426
+ details: {
427
+ operatorName: '$jsonSchema',
428
+ title: 'Email validation',
429
+ schemaRulesNotSatisfied: [
430
+ {
431
+ operatorName: 'properties',
432
+ propertiesNotSatisfied: [
433
+ {
434
+ propertyName: 'email',
435
+ description: "Email address must end with '@mongodb.com'",
436
+ details: [
437
+ {
438
+ operatorName: 'pattern',
439
+ specifiedAs: { pattern: '^@mongodb.com$' },
440
+ reason: 'regular expression did not match',
441
+ consideredValue: '
[email protected] '
442
+ }
443
+ ]
444
+ }
445
+ ]
446
+ }
447
+ ]
448
+ }
449
+ }
450
+
451
+ .. note::
452
+
453
+ The validation error output is formatted differently in the legacy
454
+ ``mongo`` shell.
455
+
387
456
Restrictions
388
457
------------
389
458
0 commit comments