Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified source/images/compass/validation-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 49 additions & 43 deletions source/validation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ To specify JSON Schema validation, use the
:manual:`$jsonSchema </reference/operator/query/jsonSchema>`
operator.

.. code-block:: json
.. code-block:: javascript

{
$jsonSchema: {
required: ['customer'], // the customer field is required
required: ['name', 'borough'], // the name and borough fields are required
properties: {
purchaseMethod: {
enum: ['In Store','Online'],
description: "can only be either 'In Store' or 'Online'"
}
cuisine: {
bsonType: "string",
description: "must be a string"
}
}
}
}
Expand All @@ -72,61 +72,68 @@ example:
- The ``required`` array defines required fields in your document.

- The ``properties`` object defines rules for specific document
fields.
fields.

Consider the following example validation:

.. code-block:: json
.. code-block:: javascript

{
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "gpa", "address.city", "address.street" ],
required: [ "address", "borough", "name" ],
properties: {
name: {
bsonType: "string",
description: "must be a string"
address: {
bsonType: "object",
properties: {
coord: {
bsonType: "array",
items: [
{
bsonType: "double",
minimum: -180,
maximum: 180,
exclusiveMaximum: false,
description: "must be a number in [ -180, 180 ]"
},
{
bsonType: "double",
minimum: -90,
maximum: 90,
exclusiveMaximum: false,
description: "must be a number in [ -90, 90 ]"
}
]
}
},
description: "must be an object"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
exclusiveMaximum: false,
description: "must be an integer in [ 2017, 3017 ]"
},
major: {
borough: {
bsonType: "string",
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values"
},
gpa: {
bsonType: [ "double" ],
minimum: 0,
description: "must be a double"
enum: [ "Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island" ],
description: "must be one of the enum strings"
}
}
}
}


This validation specifies:

- The list of
:manual:`required </reference/operator/query/jsonSchema/#available-keywords>`
fields.

- The :manual:`bsonType </reference/operator/query/jsonSchema/#available-keywords>` for
all fields.
all required fields.

- The :manual:`minimum </reference/operator/query/jsonSchema/#available-keywords>`
and :manual:`maximum </reference/operator/query/jsonSchema/#available-keywords>`
values for the ``year`` field.
values in the ``address.coord`` array.

- The acceptable values for the ``major`` field, using
- The acceptable values for the ``borough`` field, using
:manual:`enum </reference/operator/query/jsonSchema/#available-keywords>`.

- The :manual:`minimum </reference/operator/query/jsonSchema/#available-keywords>`
value for the ``gpa`` field.

For all available ``$jsonSchema`` keywords, refer to the
:manual:`$jsonSchema </reference/operator/query/jsonSchema>` page in
the MongoDB manual.
Expand All @@ -139,25 +146,24 @@ You can also specify validation using
exception of the following query operators: :query:`$near`,
:query:`$nearSphere`, :query:`$text`, and :query:`$where`.

.. code-block:: json
.. code-block:: javascript

{
$or: [
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
{ name: { $type: "string" } },
{ borough: {
bsonType: "string",
enum: [ "Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island" ],
description: "must be one of the enum strings"
} }
]
}

Using this validation, *one* of the following must be true:

- The ``phone`` field must be BSON type string,

- The ``email`` field must match the
:manual:`regex </reference/operator/query/regex/>`
``/@mongodb\.com$/``, or
- The ``name`` field must be BSON type string.
- The ``borough`` field must be one of the enum strings.

- The ``status`` field must be either ``Unknown`` or ``Incomplete``.

.. _validation-actions-levels:

Expand Down
Loading