Skip to content

Commit ffaa7ca

Browse files
christophstroblmp911de
authored andcommitted
Remove duplicate JSON Schema section from reference documentation.
Closes: spring-projects#3573 Original pull request: spring-projects#3574.
1 parent 2ef9844 commit ffaa7ca

File tree

1 file changed

+1
-182
lines changed

1 file changed

+1
-182
lines changed

src/main/asciidoc/reference/mongodb.adoc

+1-182
Original file line numberDiff line numberDiff line change
@@ -1904,8 +1904,6 @@ AggregationResults<TagCount> results = template.aggregate(aggregation, "tags", T
19041904

19051905
WARNING: Indexes are only used if the collation used for the operation matches the index collation.
19061906

1907-
include::./mongo-json-schema.adoc[leveloffset=+1]
1908-
19091907
<<mongo.repositories>> support `Collations` via the `collation` attribute of the `@Query` annotation.
19101908

19111909
.Collation support for Repositories
@@ -1946,186 +1944,7 @@ as shown in (1) and (2), will be included when creating the index.
19461944
TIP: The most specifc `Collation` outroules potentially defined others. Which means Method argument over query method annotation over doamin type annotation.
19471945
====
19481946

1949-
[[mongo.jsonSchema]]
1950-
=== JSON Schema
1951-
1952-
As of version 3.6, MongoDB supports collections that validate documents against a provided https://docs.mongodb.com/manual/core/schema-validation/#json-schema[JSON Schema].
1953-
The schema itself and both validation action and level can be defined when creating the collection, as the following example shows:
1954-
1955-
.Sample JSON schema
1956-
====
1957-
[source,json]
1958-
----
1959-
{
1960-
"type": "object", <1>
1961-
1962-
"required": [ "firstname", "lastname" ], <2>
1963-
1964-
"properties": { <3>
1965-
1966-
"firstname": { <4>
1967-
"type": "string",
1968-
"enum": [ "luke", "han" ]
1969-
},
1970-
"address": { <5>
1971-
"type": "object",
1972-
"properties": {
1973-
"postCode": { "type": "string", "minLength": 4, "maxLength": 5 }
1974-
}
1975-
}
1976-
}
1977-
}
1978-
----
1979-
<1> JSON schema documents always describe a whole document from its root. A schema is a schema object itself that can contain
1980-
embedded schema objects that describe properties and subdocuments.
1981-
<2> `required` is a property that describes which properties are required in a document. It can be specified optionally, along with other
1982-
schema constraints. See MongoDB's documentation on https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/#available-keywords[available keywords].
1983-
<3> `properties` is related to a schema object that describes an `object` type. It contains property-specific schema constraints.
1984-
<4> `firstname` specifies constraints for the `firsname` field inside the document. Here, it is a string-based `properties` element declaring
1985-
possible field values.
1986-
<5> `address` is a subdocument defining a schema for values in its `postCode` field.
1987-
====
1988-
1989-
You can provide a schema either by specifying a schema document (that is, by using the `Document` API to parse or build 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. The following example shows how use `MongoJsonSchema.builder()` to create a JSON schema:
1990-
1991-
.Creating a JSON schema
1992-
====
1993-
[source,java]
1994-
----
1995-
MongoJsonSchema.builder() <1>
1996-
.required("firstname", "lastname") <2>
1997-
1998-
.properties(
1999-
string("firstname").possibleValues("luke", "han"), <3>
2000-
2001-
object("address")
2002-
.properties(string("postCode").minLength(4).maxLength(5)))
2003-
2004-
.build(); <4>
2005-
----
2006-
<1> Obtain a schema builder to configure the schema with a fluent API.
2007-
<2> Configure required properties.
2008-
<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 entry points such as `string(…)`.
2009-
<4> Build the schema object. Use the schema to create either a collection or <<mongodb-template-query.criteria,query documents>>.
2010-
====
2011-
2012-
There are already some predefined and strongly typed schema objects (`JsonSchemaObject` and `JsonSchemaProperty`) available
2013-
through static methods on the gateway interfaces.
2014-
However, you may need to build custom property validation rules, which can be created through the builder API, as the following example shows:
2015-
2016-
[source,java]
2017-
----
2018-
// "birthdate" : { "bsonType": "date" }
2019-
JsonSchemaProperty.named("birthdate").ofType(Type.dateType());
2020-
2021-
// "birthdate" : { "bsonType": "date", "description", "Must be a date" }
2022-
JsonSchemaProperty.named("birthdate").with(JsonSchemaObject.of(Type.dateType()).description("Must be a date"));
2023-
----
2024-
2025-
The Schema builder also provides support for https://docs.mongodb.com/manual/core/security-client-side-encryption/[Client-Side Field Level Encryption]. Please refer to <<mongo.jsonSchema.encrypted-fields>> for more information,
2026-
2027-
`CollectionOptions` provides the entry point to schema support for collections, as the following example shows:
2028-
2029-
.Create collection with `$jsonSchema`
2030-
====
2031-
[source,java]
2032-
----
2033-
MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();
2034-
2035-
template.createCollection(Person.class, CollectionOptions.empty().schema(schema));
2036-
----
2037-
====
2038-
2039-
You can use a schema to query any collection for documents that match a given structure defined by a JSON schema, as the following example shows:
2040-
2041-
.Query for Documents matching a `$jsonSchema`
2042-
====
2043-
[source,java]
2044-
----
2045-
MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();
2046-
2047-
template.find(query(matchingDocumentStructure(schema)), Person.class);
2048-
----
2049-
====
2050-
2051-
The following table shows the supported JSON schema types:
2052-
2053-
[cols="3,1,6", options="header"]
2054-
.Supported JSON schema types
2055-
|===
2056-
| Schema Type
2057-
| Java Type
2058-
| Schema Properties
2059-
2060-
| `untyped`
2061-
| -
2062-
| `description`, generated `description`, `enum`, `allOf`, `anyOf`, `oneOf`, `not`
2063-
2064-
| `object`
2065-
| `Object`
2066-
| `required`, `additionalProperties`, `properties`, `minProperties`, `maxProperties`, `patternProperties`
2067-
2068-
| `array`
2069-
| any array except `byte[]`
2070-
| `uniqueItems`, `additionalItems`, `items`, `minItems`, `maxItems`
2071-
2072-
| `string`
2073-
| `String`
2074-
| `minLength`, `maxLentgth`, `pattern`
2075-
2076-
| `int`
2077-
| `int`, `Integer`
2078-
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
2079-
2080-
| `long`
2081-
| `long`, `Long`
2082-
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
2083-
2084-
| `double`
2085-
| `float`, `Float`, `double`, `Double`
2086-
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
2087-
2088-
| `decimal`
2089-
| `BigDecimal`
2090-
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
2091-
2092-
| `number`
2093-
| `Number`
2094-
| `multipleOf`, `minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`
2095-
2096-
| `binData`
2097-
| `byte[]`
2098-
| (none)
2099-
2100-
| `boolean`
2101-
| `boolean`, `Boolean`
2102-
| (none)
2103-
2104-
| `null`
2105-
| `null`
2106-
| (none)
2107-
2108-
| `objectId`
2109-
| `ObjectId`
2110-
| (none)
2111-
2112-
| `date`
2113-
| `java.util.Date`
2114-
| (none)
2115-
2116-
| `timestamp`
2117-
| `BsonTimestamp`
2118-
| (none)
2119-
2120-
| `regex`
2121-
| `java.util.regex.Pattern`
2122-
| (none)
2123-
2124-
|===
2125-
2126-
NOTE: `untyped` is a generic type that is inherited by all typed schema types. It provides all `untyped` schema properties to typed schema types.
2127-
2128-
For more information, see https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/#op._S_jsonSchema[$jsonSchema].
1947+
include::./mongo-json-schema.adoc[leveloffset=+1]
21291948

21301949
[[mongo.query.fluent-template-api]]
21311950
=== Fluent Template API

0 commit comments

Comments
 (0)