@@ -49,8 +49,8 @@ invalid documents:
49
49
- MongoDB allows the operation to proceed, but records the
50
50
violation in the MongoDB log.
51
51
52
- Steps : Reject Invalid Documents
53
- -------------------------------
52
+ Option 1 : Reject Invalid Documents
53
+ ----------------------------------
54
54
55
55
The following procedure shows how to create a schema validation that
56
56
rejects invalid documents.
@@ -75,12 +75,8 @@ rejects invalid documents.
75
75
},
76
76
email: {
77
77
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'"
84
80
}
85
81
}
86
82
} },
@@ -93,13 +89,20 @@ rejects invalid documents.
93
89
94
90
.. step:: Attempt to insert an invalid document.
95
91
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:
99
93
100
94
.. code-block:: javascript
101
95
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.
103
106
104
107
The operation fails with the following error:
105
108
@@ -108,22 +111,22 @@ rejects invalid documents.
108
111
109
112
MongoServerError: Document failed validation
110
113
Additional information: {
111
- failingDocumentId: ObjectId("62b384321340c60e11b98a84 "),
114
+ failingDocumentId: ObjectId("6377cca4aac957f2b77ea955 "),
112
115
details: {
113
116
operatorName: '$jsonSchema',
114
117
schemaRulesNotSatisfied: [
115
118
{
116
119
operatorName: 'properties',
117
120
propertiesNotSatisfied: [
118
121
{
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'" ,
121
124
details: [
122
125
{
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] '
127
130
}
128
131
]
129
132
}
@@ -138,8 +141,8 @@ rejects invalid documents.
138
141
}
139
142
}
140
143
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
+ -------------------------------------------------------------
143
146
144
147
The following procedure shows how to create a schema validation that
145
148
allows invalid documents, but records invalid documents in the MongoDB
@@ -165,12 +168,8 @@ log.
165
168
},
166
169
email: {
167
170
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'"
174
173
}
175
174
}
176
175
} },
@@ -183,13 +182,20 @@ log.
183
182
184
183
.. step:: Attempt to insert an invalid document.
185
184
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:
189
186
190
187
.. code-block:: javascript
191
188
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.
193
199
194
200
.. step:: Check the logs for the invalid document.
195
201
@@ -198,72 +204,60 @@ log.
198
204
199
205
.. code-block:: javascript
200
206
201
- db.adminCommand( { getLog:'global'} ).log.forEach(x => { print(x) })
207
+ db.adminCommand(
208
+ { getLog:'global'} ).log.forEach(x => { print(x) }
209
+ )
202
210
203
211
The MongoDB log includes an entry similar to the following object:
204
212
205
213
.. code-block:: bash
206
214
:copyable: false
207
215
208
216
{
209
- "t":{
210
- "$date":"2022-06-30T18:05:22.088-04 :00"
217
+ "t": {
218
+ "$date": "2022-11-18T13:30:43.607-05 :00"
211
219
},
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 "
222
230
},
223
- "name":"Amanda",
224
- "status":"Updated "
231
+ "name": "Amanda",
232
+
225
233
},
226
- "errInfo":{
227
- "failingDocumentId":{
228
- "$oid":"62be1e2273c105dde923129f "
234
+ "errInfo": {
235
+ "failingDocumentId": {
236
+ "$oid": "6377cf53d59841355cac1cd0 "
229
237
},
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"]
254
258
},
255
- {
256
- "operatorName":"required",
257
- "specifiedAs":{
258
- "required":[
259
- "phone"
260
- ]
261
- },
262
- "missingProperties":[
263
- "phone"
264
- ]
265
- }
266
- ]
259
+ "missingProperties": ["phone"]
260
+ }]
267
261
}
268
262
}
269
263
}
0 commit comments