You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mongo-3.adoc
+15Lines changed: 15 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -81,6 +81,21 @@ In order to use authentication with XML configuration use the `credentials` attr
81
81
</beans>
82
82
----
83
83
84
+
[[mongo.mongo-3.validation]]
85
+
=== Server-side Validation
86
+
87
+
MongoDB supports https://docs.mongodb.com/manual/core/schema-validation/[Schema Validation] as of version 3.2 with query operators
88
+
and as of version 3.6 JSON-schema based validation.
89
+
90
+
This chapter will point out the specialties for validation in MongoDB and how to apply JSON schema validation.
91
+
92
+
[[mongo.mongo-3.validation.json-schema]]
93
+
==== JSON Schema Validation
94
+
95
+
MongoDB 3.6 allows validation and querying of documents using JSON schema draft 4 including core specification and validation specification, with some differences. `$jsonSchema` can be used in a document validator (when creating a collection), which enforces that inserted or updated documents are valid against the schema. It can also be used to query for documents with the `find` command or `$match` aggregation stage.
96
+
97
+
Spring Data MongoDB supports MongoDB's specific JSON schema implementation to define and use schemas. See <<mongo.jsonSchema,JSON Schema>> for further details.
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mongodb.adoc
+145-6Lines changed: 145 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1054,6 +1054,8 @@ As you can see most methods return the `Criteria` object to provide a fluent sty
1054
1054
* `Criteria` *regex* `(String re)` Creates a criterion using a `$regex`
1055
1055
* `Criteria` *size* `(int s)` Creates a criterion using the `$size` operator
1056
1056
* `Criteria` *type* `(int t)` Creates a criterion using the `$type` operator
1057
+
* `Criteria` *matchingDocumentStructure* `(MongoJsonSchema schema)` Creates a criterion using the `$jsonSchema` operator for <<mongo.jsonSchema,JSON schema criteria>>. `$jsonSchema` can only be applied on the top level of a query and not property specific. Use the `properties` attribute of the schema to match against nested fields.
1058
+
1057
1059
1058
1060
There are also methods on the Criteria class for geospatial queries. Here is a listing but look at the section on <<mongo.geospatial,GeoSpatial Queries>> to see them in action.
1059
1061
@@ -1467,29 +1469,166 @@ WARNING: Indexes are only used if the collation used for the operation and the i
1467
1469
[[mongo.jsonSchema]]
1468
1470
=== JSON Schema
1469
1471
1470
-
As of version 3.6 MongoDB supports collections that validate ``Document``s against a provided JSON Schema. The schema itself and both validation action and level can be defined when creating the collection.
1472
+
As of version 3.6 MongoDB supports collections that validate ``Document``s against a provided JSON Schema.
1473
+
The schema itself and both validation action and level can be defined when creating the collection.
<1> JSON schema documents always describe a whole document from its root. A schema is a schema object itself that can contain
1500
+
embedded schema objects describing properties and subdocuments.
1501
+
<2> `required` is a property describing which properties are required in a document. It can be specified optionally along of other
1502
+
schema constraints. See MongoDB's documentation on https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/#available-keywords[available keywords].
1503
+
<3> `properties` is related to a schema object describing an `object` type. It contains property-specific schema constraints.
1504
+
<4> `firstname` specifies constrains for the `firsname` field inside the document. Here it's a string-based properties declaring
1505
+
possible field values.
1506
+
<5> `address` is a subdocument defining a schema for values in its `postCode` field.
1507
+
====
1508
+
1509
+
You can provide a schema either by specifying a schema document (i.e. using the `Document` API by parsing or building a document object) or by building it with Spring Data's JSON schema utilities in `org.springframework.data.mongodb.core.schema`. `MongoJsonSchema` is the entry point for all JSON schema-related operations.
<1> Obtain a schema builder to configure the schema with a fluent API.
1527
+
<2> Configure required properties.
1528
+
<3> Configure the String-typed `firstname` field allowing only `luke` and `han` values. Properties can be typed or untyped. Use a static import of `JsonSchemaProperty` to make the syntax slightly more compact and to get entrypoints like `string(…)`.
1529
+
<4> Build the schema object. Use the schema to either create a collection or <<mongodb-template-query.criteria,query documents>>.
1530
+
====
1471
1531
1472
1532
`CollectionOptions` provides the entry point to schema support for collections.
NOTE: `$jsonSchema` can only be applied on the top level of a query and not property specific. Use the `properties` attribute of the schema to match against nested fields.
0 commit comments