Details
-
Task
-
Resolution: Fixed
-
Major - P3
-
None
Description
Description
Hi team,
I discovered an incorrect regex example in our documentation regarding schema validation. https://docs.mongodb.com/manual/core/schema-validation/#accept-or-reject-invalid-documents
```
email:
{ bsonType : "string", pattern : "@mongodb\.com$", description: "must be a string and match the regular expression pattern" }```
This part may lead the readers to believe that the regex expression defined in "pattern" will match any string ends with "@mongodb.com", however, the regex above will also match "@mongodbxcom" or "@mongodbbcom"
The correct expression when used with "pattern" should be "@mongodb\\.com$" because the `\` must be escaped and replaced with \\ in a JSON string.
Test 1: shows the current example accepts "@mongodbb.com"
```
MongoDB Enterprise >
|
db.getSiblingDB('TSA').createCollection("test", {validator: {
|
$jsonSchema:{
|
bsonType: "object",
|
properties:
|
{
|
emailsuffix: { bsonType:"string", pattern: "@mongodb\.com$"}
|
}}}})
|
|
|
{ "ok" : 1 }
|
MongoDB Enterprise >
|
db.getSiblingDB('TSA').test.insert(
|
{emailsuffix: "abcd@mongodbbcom"}
|
)
|
|
|
WriteResult(\{ "nInserted" : 1 })
|
```
Test 2: shows with the correctly escaped '\' it won't accept "@mongodbb.com", and accepts only "@mongodb.com"
```
MongoDB Enterprise > db.getSiblingDB('TSA').test.drop()
true
MongoDB Enterprise >
|
db.getSiblingDB('TSA').createCollection("test", {validator: {
|
$jsonSchema:{
|
bsonType: "object",
|
properties: {
|
emailsuffix: { bsonType:"string", pattern: "@mongodb\\.com$"}
|
}
|
}}})
|
|
|
{ "ok" : 1 }
|
MongoDB Enterprise > db.getSiblingDB('TSA').test.insert(\{emailsuffix: "abcd@mongodbbcom"})
|
|
|
WriteResult(\{ "nInserted" : 0, "writeError" : { "code" : 121, "errmsg" : "Document failed validation" }})
|
|
|
|
|
MongoDB Enterprise > db.getSiblingDB('TSA').test.insert(\{emailsuffix: "abcd@mongodb.com"})
|
|
|
WriteResult(\{ "nInserted" : 1 })
|
Best regards,
Raymond