Skip to content

Commit

Permalink
DOCSP-36179-schema-validation-updates (#6156)
Browse files Browse the repository at this point in the history
* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

* DOCSP-36179-schema-validation-updates

---------

Co-authored-by: jason-price-mongodb <[email protected]>
  • Loading branch information
jason-price-mongodb and jason-price-mongodb authored Feb 7, 2024
1 parent b7bcd78 commit e511159
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
20 changes: 7 additions & 13 deletions source/core/schema-validation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,20 @@ validation in the following scenarios:
from accidentally misspelling an item name when entering sales data.

- For a students collection, ensure that the ``gpa`` field is always a
positive number. This validation catches typos during data entry.
positive number. This validation prevents errors during data entry.

When MongoDB Checks Validation
------------------------------

When you create a new collection with schema validation, MongoDB checks
validation during updates and inserts in that collection.
After you add schema validation rules to a collection:

When you add validation to an existing, non-empty collection:

- Newly inserted documents are checked for validation.

- Documents already existing in your collection are not checked for
validation until they are modified. Specific behavior for existing
documents depends on your chosen validation level. To learn more, see
- All document inserts must match the rules.
- The schema validation level defines how the rules are applied to
existing documents and document updates. To learn more, see
:ref:`schema-specify-validation-level`.

Adding validation to an existing collection does not enforce validation
on existing documents. To check a collection for invalid documents, use
the :dbcommand:`validate` command.
To find documents in a collection that don't match the schema validation
rules, see :ref:`use-json-schema-query-conditions-find-documents`.

What Happens When a Document Fails Validation
---------------------------------------------
Expand Down
14 changes: 8 additions & 6 deletions source/core/schema-validation/specify-validation-level.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ MongoDB applies validation rules:
- Behavior

* - ``strict``
- (*Default*) MongoDB applies validation rules to all inserts and
updates.
- (*Default*) MongoDB applies the same validation rules to all
document inserts and updates.

* - ``moderate``
- MongoDB only applies validation rules to existing valid
documents. Updates to invalid documents which exist prior to the
validation being added are not checked for validity.
- MongoDB applies the same validation rules to document inserts
and updates to existing valid documents that match the
validation rules. Updates to existing documents in the
collection that don't match the validation rules aren't checked
for validity.

Prerequisite
------------
Expand Down Expand Up @@ -260,7 +262,7 @@ documents.
upsertedCount: 0
}

The output shows that:
The output shows:

- The update fails for the document with ``_id: 1``. This document
met the initial validation requirements, and MongoDB applies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,14 @@ Both commands return the same result:
}
]

.. _use-json-schema-query-conditions-find-documents:

Find Documents that Don't Match the Schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To find all documents do not satisfy the schema:, use
:query:`$jsonSchema` with the :query:`$nor` operator:
To find documents in a collection that don't match the schema validation
rules, use :query:`$jsonSchema` with the :query:`$nor` operator. For
example:

.. code-block:: javascript

Expand Down

0 comments on commit e511159

Please sign in to comment.