From e5111594d1f2d49aecc4fd8738df74acfb36b433 Mon Sep 17 00:00:00 2001 From: jason-price-mongodb <69260375+jason-price-mongodb@users.noreply.github.com> Date: Wed, 7 Feb 2024 09:53:28 -0800 Subject: [PATCH] DOCSP-36179-schema-validation-updates (#6156) * 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 --- source/core/schema-validation.txt | 20 +++++++------------ .../specify-validation-level.txt | 14 +++++++------ .../use-json-schema-query-conditions.txt | 7 +++++-- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/source/core/schema-validation.txt b/source/core/schema-validation.txt index ad82dd137d2..2f69e5fbd49 100644 --- a/source/core/schema-validation.txt +++ b/source/core/schema-validation.txt @@ -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 --------------------------------------------- diff --git a/source/core/schema-validation/specify-validation-level.txt b/source/core/schema-validation/specify-validation-level.txt index f563a3fe7bb..2293762008a 100644 --- a/source/core/schema-validation/specify-validation-level.txt +++ b/source/core/schema-validation/specify-validation-level.txt @@ -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 ------------ @@ -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 diff --git a/source/core/schema-validation/use-json-schema-query-conditions.txt b/source/core/schema-validation/use-json-schema-query-conditions.txt index 8f1b64de4c1..9f62e731fdf 100644 --- a/source/core/schema-validation/use-json-schema-query-conditions.txt +++ b/source/core/schema-validation/use-json-schema-query-conditions.txt @@ -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