Skip to content

Commit 365430c

Browse files
committed
DATAMONGO-1835 - Documentation.
Original pull request: #524.
1 parent 14ccb51 commit 365430c

File tree

4 files changed

+193
-8
lines changed

4 files changed

+193
-8
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/MongoJsonSchema.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@
2323
import org.springframework.util.Assert;
2424

2525
/**
26+
* Interface defining MongoDB-specific JSON schema object. New objects can be built with {@link #builder()}, for
27+
* example:
28+
*
29+
* <pre class="code">
30+
* MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname")
31+
* .properties(string("firstname").possibleValues("luke", "han"),
32+
* object("address").properties(string("postCode").minLength(4).maxLength(5))
33+
*
34+
* ).build();
35+
* </pre>
36+
*
37+
* resulting in the following schema:
38+
*
39+
* <pre>
40+
* {
41+
"type": "object",
42+
"required": [ "firstname", "lastname" ],
43+
"properties": {
44+
"firstname": {
45+
"type": "string", "enum": [ "luke", "han" ],
46+
},
47+
"address": {
48+
"type": "object",
49+
"properties": {
50+
"postCode": { "type": "string", "minLength": 4, "maxLength": 5 }
51+
}
52+
}
53+
}
54+
}
55+
* </pre>
56+
*
2657
* @author Christoph Strobl
2758
* @since 2.1
2859
**/

src/main/asciidoc/new-features.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
== What's new in Spring Data MongoDB 2.1
66
* Cursor-based aggregation execution.
77
* <<mongo-template.query.distinct,Distinct queries>> for imperative and reactive Template API.
8-
* `validator` support for collections.
9-
* `$jsonSchema` support for queries.
8+
* <<mongo.mongo-3.validation,`validator` support for collections>>.
9+
* <<mongo.jsonSchema,`$jsonSchema` support>> for queries and collection creation.
1010

1111
[[new-features.2-0-0]]
1212
== What's new in Spring Data MongoDB 2.0

src/main/asciidoc/reference/mongo-3.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ In order to use authentication with XML configuration use the `credentials` attr
8181
</beans>
8282
----
8383

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.
98+
8499
[[mongo.mongo-3.misc]]
85100
=== Other things to be aware of
86101

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 145 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,8 @@ As you can see most methods return the `Criteria` object to provide a fluent sty
10541054
* `Criteria` *regex* `(String re)` Creates a criterion using a `$regex`
10551055
* `Criteria` *size* `(int s)` Creates a criterion using the `$size` operator
10561056
* `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+
10571059

10581060
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.
10591061

@@ -1467,29 +1469,166 @@ WARNING: Indexes are only used if the collation used for the operation and the i
14671469
[[mongo.jsonSchema]]
14681470
=== JSON Schema
14691471

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.
1474+
1475+
.Sample JSON schema
1476+
====
1477+
[source,json]
1478+
----
1479+
{
1480+
"type": "object", <1>
1481+
1482+
"required": [ "firstname", "lastname" ], <2>
1483+
1484+
"properties": { <3>
1485+
1486+
"firstname": { <4>
1487+
"type": "string",
1488+
"enum": [ "luke", "han" ]
1489+
},
1490+
"address": { <5>
1491+
"type": "object",
1492+
"properties": {
1493+
"postCode": { "type": "string", "minLength": 4, "maxLength": 5 }
1494+
}
1495+
}
1496+
}
1497+
}
1498+
----
1499+
<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.
1510+
1511+
.Creating a JSON schema
1512+
====
1513+
[source,java]
1514+
----
1515+
MongoJsonSchema.builder() <1>
1516+
.required("firstname", "lastname") <2>
1517+
1518+
.properties(
1519+
string("firstname").possibleValues("luke", "han"), <3>
1520+
1521+
object("address")
1522+
.properties(string("postCode").minLength(4).maxLength(5)))
1523+
1524+
.build(); <4>
1525+
----
1526+
<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+
====
14711531

14721532
`CollectionOptions` provides the entry point to schema support for collections.
14731533

14741534
.Create collection with `$jsonSchema`
14751535
====
14761536
[source,java]
14771537
----
1478-
// TODO: add sample here!
1538+
MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();
1539+
1540+
template.createCollection(Person.class, CollectionOptions.empty().schema(schema));
14791541
----
14801542
====
14811543

1482-
Additionally it is also possible to query any collection for documents that match a given structure defined by a JSON Schema.
1544+
You can use a schema to query any collection for documents that match a given structure defined by a JSON schema.
14831545

1484-
.Query collation for Documents matching a `$jsonSchema`
1546+
.Query for Documents matching a `$jsonSchema`
14851547
====
14861548
[source,java]
14871549
----
1488-
// TODO: add sample here!
1550+
MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();
1551+
1552+
template.find(query(matchingDocumentStructure(schema)), Person.class);
14891553
----
14901554
====
14911555

1492-
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.
1556+
[cols="3,1,6", options="header"]
1557+
.Supported JSON schema types
1558+
|===
1559+
| Schema Type
1560+
| Java Type
1561+
| Schema Properties
1562+
1563+
| `untyped`
1564+
| -
1565+
| `description`, generated `description`, `enum`, `allOf`, `anyOf`, `oneOf`, `not`
1566+
1567+
| `object`
1568+
| `Object`
1569+
| `required`, `additionalProperties`, `properties`, `minProperties`, `maxProperties`, `patternProperties`
1570+
1571+
| `array`
1572+
| any array except `byte[]`
1573+
| `uniqueItems`, `additionalItems`, `items`, `minItems`, `maxItems`
1574+
1575+
| `string`
1576+
| `String`
1577+
| `minLength`, `maxLentgth`, `pattern`
1578+
1579+
| `int`
1580+
| `int`, `Integer`
1581+
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
1582+
1583+
| `long`
1584+
| `long`, `Long`
1585+
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
1586+
1587+
| `double`
1588+
| `float`, `Float`, `double`, `Double`
1589+
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
1590+
1591+
| `decimal`
1592+
| `BigDecimal`
1593+
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
1594+
1595+
| `number`
1596+
| `Number`
1597+
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
1598+
1599+
| `binData`
1600+
| `byte[]`
1601+
|
1602+
1603+
| `boolean`
1604+
| `boolean`, `Boolean`
1605+
|
1606+
1607+
| `null`
1608+
| `null`
1609+
|
1610+
1611+
| `objectId`
1612+
| `ObjectId`
1613+
|
1614+
1615+
| `date`
1616+
| `java.util.Date`
1617+
|
1618+
1619+
| `timestamp`
1620+
| `BsonTimestamp`
1621+
|
1622+
1623+
| `regex`
1624+
| `java.util.regex.Pattern`
1625+
|
1626+
1627+
|===
1628+
1629+
NOTE: `untyped` is a generic type that is inherited by all typed schema types providing all `untyped` schema properties to typed schema types.
1630+
1631+
For more information, see https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/#op._S_jsonSchema[$jsonSchema].
14931632

14941633
[[mongo.query.fluent-template-api]]
14951634
=== Fluent Template API

0 commit comments

Comments
 (0)