Skip to content

Commit 7a67e8e

Browse files
(DOCS-14355): fix regex pattern in validation example (#2166)
* (DOCS-14355): fix regex pattern in validation example * change example * typo * fix formatting * remove extra sentence * formatting * consistency * update section titles per review
1 parent ca6da51 commit 7a67e8e

File tree

1 file changed

+79
-85
lines changed

1 file changed

+79
-85
lines changed

source/core/schema-validation/handle-invalid-documents.txt

Lines changed: 79 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ invalid documents:
4949
- MongoDB allows the operation to proceed, but records the
5050
violation in the MongoDB log.
5151

52-
Steps: Reject Invalid Documents
53-
-------------------------------
52+
Option 1: Reject Invalid Documents
53+
----------------------------------
5454

5555
The following procedure shows how to create a schema validation that
5656
rejects invalid documents.
@@ -75,12 +75,8 @@ rejects invalid documents.
7575
},
7676
email: {
7777
bsonType : "string",
78-
pattern : "@mongodb\.com$",
79-
description: "must be a string and match the regular expression pattern"
80-
},
81-
status: {
82-
enum: [ "Unknown", "Incomplete" ],
83-
description: "can only be one of the enum values"
78+
pattern : "@mongodb\\.com$",
79+
description: "must be a string and end with '@mongodb.com'"
8480
}
8581
}
8682
} },
@@ -93,13 +89,20 @@ rejects invalid documents.
9389

9490
.. step:: Attempt to insert an invalid document.
9591

96-
The following insert operation violates the validation rule because
97-
``status`` is not one of the allowed values (``Unknown`` or
98-
``Incomplete``):
92+
Attempt to insert the following document:
9993

10094
.. code-block:: javascript
10195

102-
db.contacts.insertOne( { name: "Amanda", status: "Updated" } )
96+
db.contacts.insertOne(
97+
{ name: "Amanda", email: "[email protected]" }
98+
)
99+
100+
The document violates the validation rule because:
101+
102+
- The ``email`` field does not match the regular expression
103+
pattern. The ``email`` field must end in ``@mongodb.com``.
104+
105+
- It is missing the required ``phone`` field.
103106

104107
The operation fails with the following error:
105108

@@ -108,22 +111,22 @@ rejects invalid documents.
108111

109112
MongoServerError: Document failed validation
110113
Additional information: {
111-
failingDocumentId: ObjectId("62b384321340c60e11b98a84"),
114+
failingDocumentId: ObjectId("6377cca4aac957f2b77ea955"),
112115
details: {
113116
operatorName: '$jsonSchema',
114117
schemaRulesNotSatisfied: [
115118
{
116119
operatorName: 'properties',
117120
propertiesNotSatisfied: [
118121
{
119-
propertyName: 'status',
120-
description: 'can only be one of the enum values',
122+
propertyName: 'email',
123+
description: "must be a string and end with '@mongodb.com'",
121124
details: [
122125
{
123-
operatorName: 'enum',
124-
specifiedAs: { enum: [ 'Unknown', 'Incomplete' ] },
125-
reason: 'value was not found in enum',
126-
consideredValue: 'Updated'
126+
operatorName: 'pattern',
127+
specifiedAs: { pattern: '@mongodb\\.com$' },
128+
reason: 'regular expression did not match',
129+
consideredValue: '[email protected]'
127130
}
128131
]
129132
}
@@ -138,8 +141,8 @@ rejects invalid documents.
138141
}
139142
}
140143

141-
Steps: Allow Invalid Documents and Record Them in the Log
142-
---------------------------------------------------------
144+
Option 2: Allow Invalid Documents, but Record Them in the Log
145+
-------------------------------------------------------------
143146

144147
The following procedure shows how to create a schema validation that
145148
allows invalid documents, but records invalid documents in the MongoDB
@@ -165,12 +168,8 @@ log.
165168
},
166169
email: {
167170
bsonType : "string",
168-
pattern : "@mongodb\.com$",
169-
description: "must be a string and match the regular expression pattern"
170-
},
171-
status: {
172-
enum: [ "Unknown", "Incomplete" ],
173-
description: "can only be one of the enum values"
171+
pattern : "@mongodb\\.com$",
172+
description: "must be a string and end with '@mongodb.com'"
174173
}
175174
}
176175
} },
@@ -183,13 +182,20 @@ log.
183182

184183
.. step:: Attempt to insert an invalid document.
185184

186-
The following insert operation violates the validation rule because
187-
``status`` is not one of the allowed values (``Unknown`` or
188-
``Incomplete``):
185+
Attempt to insert the following document:
189186

190187
.. code-block:: javascript
191188

192-
db.contacts2.insertOne( { name: "Amanda", status: "Updated" } )
189+
db.contacts2.insertOne(
190+
{ name: "Amanda", email: "[email protected]" }
191+
)
192+
193+
The document violates the validation rule because:
194+
195+
- The ``email`` field does not match the regular expression
196+
pattern. The ``email`` field must end in ``@mongodb.com``.
197+
198+
- It is missing the required ``phone`` field.
193199

194200
.. step:: Check the logs for the invalid document.
195201

@@ -198,72 +204,60 @@ log.
198204

199205
.. code-block:: javascript
200206

201-
db.adminCommand( { getLog:'global'} ).log.forEach(x => { print(x) })
207+
db.adminCommand(
208+
{ getLog:'global'} ).log.forEach(x => { print(x) }
209+
)
202210

203211
The MongoDB log includes an entry similar to the following object:
204212

205213
.. code-block:: bash
206214
:copyable: false
207215

208216
{
209-
"t":{
210-
"$date":"2022-06-30T18:05:22.088-04:00"
217+
"t": {
218+
"$date": "2022-11-18T13:30:43.607-05:00"
211219
},
212-
"s":"W",
213-
"c":"STORAGE",
214-
"id":20294,
215-
"ctx":"conn61",
216-
"msg":"Document would fail validation",
217-
"attr":{
218-
"namespace":"test.contacts2",
219-
"document":{
220-
"_id":{
221-
"$oid":"62be1e2273c105dde923129f"
220+
"s": "W",
221+
"c": "STORAGE",
222+
"id": 20294,
223+
"ctx": "conn2",
224+
"msg": "Document would fail validation",
225+
"attr": {
226+
"namespace": "test.contacts2",
227+
"document": {
228+
"_id": {
229+
"$oid": "6377cf53d59841355cac1cd0"
222230
},
223-
"name":"Amanda",
224-
"status":"Updated"
231+
"name": "Amanda",
232+
"email": "[email protected]"
225233
},
226-
"errInfo":{
227-
"failingDocumentId":{
228-
"$oid":"62be1e2273c105dde923129f"
234+
"errInfo": {
235+
"failingDocumentId": {
236+
"$oid": "6377cf53d59841355cac1cd0"
229237
},
230-
"details":{
231-
"operatorName":"$jsonSchema",
232-
"schemaRulesNotSatisfied":[
233-
{
234-
"operatorName":"properties",
235-
"propertiesNotSatisfied":[
236-
{
237-
"propertyName":"status",
238-
"description":"can only be one of the enum values",
239-
"details":[
240-
{
241-
"operatorName":"enum",
242-
"specifiedAs":{
243-
"enum":[
244-
"Unknown",
245-
"Incomplete"
246-
]
247-
},
248-
"reason":"value was not found in enum",
249-
"consideredValue":"Updated"
250-
}
251-
]
252-
}
253-
]
238+
"details": {
239+
"operatorName": "$jsonSchema",
240+
"schemaRulesNotSatisfied": [{
241+
"operatorName": "properties",
242+
"propertiesNotSatisfied": [{
243+
"propertyName": "email",
244+
"description": "must be a string and end with '@mongodb.com'",
245+
"details": [{
246+
"operatorName": "pattern",
247+
"specifiedAs": {
248+
"pattern": "@mongodb\\.com$"
249+
},
250+
"reason": "regular expression did not match",
251+
"consideredValue": "[email protected]"
252+
}]
253+
}]
254+
}, {
255+
"operatorName": "required",
256+
"specifiedAs": {
257+
"required": ["phone"]
254258
},
255-
{
256-
"operatorName":"required",
257-
"specifiedAs":{
258-
"required":[
259-
"phone"
260-
]
261-
},
262-
"missingProperties":[
263-
"phone"
264-
]
265-
}
266-
]
259+
"missingProperties": ["phone"]
260+
}]
267261
}
268262
}
269263
}

0 commit comments

Comments
 (0)