Skip to content

Commit d3624b9

Browse files
christophstroblmp911de
authored andcommitted
Remove duplicate JSON Schema section from reference documentation.
Closes: #3573 Original pull request: #3574.
1 parent 3711ed5 commit d3624b9

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
@@ -1886,8 +1886,6 @@ AggregationResults<TagCount> results = template.aggregate(aggregation, "tags", T
18861886

18871887
WARNING: Indexes are only used if the collation used for the operation matches the index collation.
18881888

1889-
include::./mongo-json-schema.adoc[leveloffset=+1]
1890-
18911889
<<mongo.repositories>> support `Collations` via the `collation` attribute of the `@Query` annotation.
18921890

18931891
.Collation support for Repositories
@@ -1928,186 +1926,7 @@ as shown in (1) and (2), will be included when creating the index.
19281926
TIP: The most specifc `Collation` outroules potentially defined others. Which means Method argument over query method annotation over doamin type annotation.
19291927
====
19301928

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

21121931
[[mongo.query.fluent-template-api]]
21131932
=== Fluent Template API

0 commit comments

Comments
 (0)