@@ -51,16 +51,16 @@ To specify JSON Schema validation, use the
51
51
:manual:`$jsonSchema </reference/operator/query/jsonSchema>`
52
52
operator.
53
53
54
- .. code-block:: json
54
+ .. code-block:: javascript
55
55
56
56
{
57
57
$jsonSchema: {
58
- required: ['customer' ], // the customer field is required
58
+ required: ['name', 'borough' ], // the name and borough fields are required
59
59
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
+ }
64
64
}
65
65
}
66
66
}
@@ -72,61 +72,68 @@ example:
72
72
- The ``required`` array defines required fields in your document.
73
73
74
74
- The ``properties`` object defines rules for specific document
75
- fields.
75
+ fields.
76
76
77
77
Consider the following example validation:
78
78
79
- .. code-block:: json
79
+ .. code-block:: javascript
80
80
81
81
{
82
82
$jsonSchema: {
83
83
bsonType: "object",
84
- required: [ "name ", "year ", "major", "gpa", "address.city", "address.street " ],
84
+ required: [ "address ", "borough ", "name " ],
85
85
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"
89
110
},
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: {
98
112
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"
106
115
}
107
116
}
108
117
}
109
118
}
110
119
120
+
111
121
This validation specifies:
112
122
113
123
- The list of
114
124
:manual:`required </reference/operator/query/jsonSchema/#available-keywords>`
115
125
fields.
116
126
117
127
- The :manual:`bsonType </reference/operator/query/jsonSchema/#available-keywords>` for
118
- all fields.
128
+ all required fields.
119
129
120
130
- The :manual:`minimum </reference/operator/query/jsonSchema/#available-keywords>`
121
131
and :manual:`maximum </reference/operator/query/jsonSchema/#available-keywords>`
122
- values for the ``year `` field .
132
+ values in the ``address.coord `` array .
123
133
124
- - The acceptable values for the ``major `` field, using
134
+ - The acceptable values for the ``borough `` field, using
125
135
:manual:`enum </reference/operator/query/jsonSchema/#available-keywords>`.
126
136
127
- - The :manual:`minimum </reference/operator/query/jsonSchema/#available-keywords>`
128
- value for the ``gpa`` field.
129
-
130
137
For all available ``$jsonSchema`` keywords, refer to the
131
138
:manual:`$jsonSchema </reference/operator/query/jsonSchema>` page in
132
139
the MongoDB manual.
@@ -139,25 +146,24 @@ You can also specify validation using
139
146
exception of the following query operators: :query:`$near`,
140
147
:query:`$nearSphere`, :query:`$text`, and :query:`$where`.
141
148
142
- .. code-block:: json
149
+ .. code-block:: javascript
143
150
144
151
{
145
152
$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
+ } }
149
159
]
150
160
}
151
161
152
162
Using this validation, *one* of the following must be true:
153
163
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.
159
166
160
- - The ``status`` field must be either ``Unknown`` or ``Incomplete``.
161
167
162
168
.. _validation-actions-levels:
163
169
0 commit comments