Browse Source

query operations

labs/antora
Christoph Strobl 2 years ago
parent
commit
ddc8843fc1
No known key found for this signature in database
GPG Key ID: 8CC1AB53391458C8
  1. 49
      src/main/antora/modules/ROOT/pages/mongodb/template-query-operations.adoc
  2. 12
      src/main/antora/modules/ROOT/pages/mongodb/template-query-options.adoc

49
src/main/antora/modules/ROOT/pages/mongodb/template-query-operations.adoc

@ -67,8 +67,9 @@ The rest of this section lists the methods of the `Criteria` and `Query` classes
Most methods return the `Criteria` object, to provide a fluent style for the API. Most methods return the `Criteria` object, to provide a fluent style for the API.
[[mongodb-template-query.criteria]] [[mongodb-template-query.criteria]]
== Methods for the Criteria Class .Methods of the Criteria Class
[%collapsible]
====
The `Criteria` class provides the following methods, all of which correspond to operators in MongoDB: The `Criteria` class provides the following methods, all of which correspond to operators in MongoDB:
* `Criteria` *all* `(Object o)` Creates a criterion using the `$all` operator * `Criteria` *all* `(Object o)` Creates a criterion using the `$all` operator
@ -99,7 +100,6 @@ The `Criteria` class provides the following methods, all of which correspond to
* `Criteria` *matchingDocumentStructure* `(MongoJsonSchema schema)` Creates a criterion using the `$jsonSchema` operator for xref:mongodb/template-collection-schema.adoc[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. * `Criteria` *matchingDocumentStructure* `(MongoJsonSchema schema)` Creates a criterion using the `$jsonSchema` operator for xref:mongodb/template-collection-schema.adoc[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.
* `Criteria` *bits()* is the gateway to https://docs.mongodb.com/manual/reference/operator/query-bitwise/[MongoDB bitwise query operators] like `$bitsAllClear`. * `Criteria` *bits()* is the gateway to https://docs.mongodb.com/manual/reference/operator/query-bitwise/[MongoDB bitwise query operators] like `$bitsAllClear`.
The Criteria class also provides the following methods for geospatial queries. The Criteria class also provides the following methods for geospatial queries.
* `Criteria` *within* `(Circle circle)` Creates a geospatial criterion using `$geoWithin $center` operators. * `Criteria` *within* `(Circle circle)` Creates a geospatial criterion using `$geoWithin $center` operators.
@ -109,19 +109,35 @@ The Criteria class also provides the following methods for geospatial queries.
* `Criteria` *nearSphere* `(Point point)` Creates a geospatial criterion using `$nearSphere$center` operations. This is only available for MongoDB 1.7 and higher. * `Criteria` *nearSphere* `(Point point)` Creates a geospatial criterion using `$nearSphere$center` operations. This is only available for MongoDB 1.7 and higher.
* `Criteria` *minDistance* `(double minDistance)` Creates a geospatial criterion using the `$minDistance` operation, for use with $near. * `Criteria` *minDistance* `(double minDistance)` Creates a geospatial criterion using the `$minDistance` operation, for use with $near.
* `Criteria` *maxDistance* `(double maxDistance)` Creates a geospatial criterion using the `$maxDistance` operation, for use with $near. * `Criteria` *maxDistance* `(double maxDistance)` Creates a geospatial criterion using the `$maxDistance` operation, for use with $near.
====
The `Query` class has some additional methods that allow to select certain fields as well as to limit and sort the result.
[[mongodb-template-query.query]] [[mongodb-template-query.query]]
== Methods for the Query class .Methods of the Query class
[%collapsible]
The `Query` class has some additional methods that provide options for the query: ====
* `Query` *addCriteria* `(Criteria criteria)` used to add additional criteria to the query * `Query` *addCriteria* `(Criteria criteria)` used to add additional criteria to the query
* `Field` *fields* `()` used to define fields to be included in the query results * `Field` *fields* `()` used to define fields to be included in the query results
* `Query` *limit* `(int limit)` used to limit the size of the returned results to the provided limit (used for paging) * `Query` *limit* `(int limit)` used to limit the size of the returned results to the provided limit (used for paging)
* `Query` *skip* `(int skip)` used to skip the provided number of documents in the results (used for paging) * `Query` *skip* `(int skip)` used to skip the provided number of documents in the results (used for paging)
* `Query` *with* `(Sort sort)` used to provide sort definition for the results * `Query` *with* `(Sort sort)` used to provide sort definition for the results
* `Query` *with* `(ScrollPosition position)` used to provide a scroll position (Offset- or Keyset-based pagination) to start or resume a `Scroll` * `Query` *with* `(ScrollPosition position)` used to provide a scroll position (Offset- or Keyset-based pagination) to start or resume a `Scroll`
====
[[mongo-template.query.result-projection]]
The template API allows direct usage of result projections that enable you to map queries against a given domain type while projecting the operation result onto another one as outlined below.
[source,java]
----
class
template.query(SWCharacter.class)
.as(Jedi.class)
----
For more information on result projections please refer to the xref:repositories/projections.adoc[Projections] section of the documentation.
[[mongo-template.querying.field-selection]] [[mongo-template.querying.field-selection]]
== Selecting fields == Selecting fields
@ -151,8 +167,6 @@ query.fields().exclude("id").include("lastname") <2>
query.fields().include("address") <3> query.fields().include("address") <3>
query.fields().include("address.city") <4> query.fields().include("address.city") <4>
---- ----
<1> Result will contain both `_id` and `last_name` via `{ "last_name" : 1 }`. <1> Result will contain both `_id` and `last_name` via `{ "last_name" : 1 }`.
<2> Result will only contain the `last_name` via `{ "_id" : 0, "last_name" : 1 }`. <2> Result will only contain the `last_name` via `{ "_id" : 0, "last_name" : 1 }`.
@ -234,6 +248,9 @@ NOTE: Using GeoSpatial queries requires attention when used within MongoDB trans
To understand how to perform GeoSpatial queries, consider the following `Venue` class (taken from the integration tests and relying on the rich `MappingMongoConverter`): To understand how to perform GeoSpatial queries, consider the following `Venue` class (taken from the integration tests and relying on the rich `MappingMongoConverter`):
.Venue.java
[%collapsible]
====
[source,java] [source,java]
---- ----
@Document(collection="newyork") @Document(collection="newyork")
@ -272,6 +289,7 @@ public class Venue {
} }
} }
---- ----
====
To find locations within a `Circle`, you can use the following query: To find locations within a `Circle`, you can use the following query:
@ -397,11 +415,7 @@ public class Store {
String id; String id;
/** /**
* location is stored in GeoJSON format. * { "type" : "Point", "coordinates" : [ x, y ] }
* {
* "type" : "Point",
* "coordinates" : [ x, y ]
* }
*/ */
GeoJsonPoint location; GeoJsonPoint location;
} }
@ -780,10 +794,12 @@ Doing so forces exact document matching for all property values and the property
Also, keep in mind that using `@TypeAlias` requires eager initialization of the `MappingContext`. To do so, configure `initialEntitySet` to to ensure proper alias resolution for read operations. Also, keep in mind that using `@TypeAlias` requires eager initialization of the `MappingContext`. To do so, configure `initialEntitySet` to to ensure proper alias resolution for read operations.
==== ====
Spring Data MongoDB provides support for the following matching options: Spring Data MongoDB provides support for different matching options:
[cols="1,2", options="header"]
.`StringMatcher` options .`StringMatcher` options
[%collapsible]
====
[cols="1,2", options="header"]
|=== |===
| Matching | Matching
| Logical result | Logical result
@ -825,6 +841,7 @@ Spring Data MongoDB provides support for the following matching options:
| `{"firstname" : { $regex: /firstname/, $options: 'i'}}` | `{"firstname" : { $regex: /firstname/, $options: 'i'}}`
|=== |===
====
[[mongo.jsonSchema.query]] [[mongo.jsonSchema.query]]
== Query a collection for matching JSON Schema == Query a collection for matching JSON Schema

12
src/main/antora/modules/ROOT/pages/mongodb/template-query-options.adoc

@ -1,11 +1,11 @@
[[mongo.query.additional-query-options]] [[mongo.query.additional-query-options]]
== Additional Query Options = Additional Query Options
MongoDB offers various ways of applying meta information, like a comment or a batch size, to a query.Using the `Query` API MongoDB offers various ways of applying meta information, like a comment or a batch size, to a query.Using the `Query` API
directly there are several methods for those options. directly there are several methods for those options.
[[mongo.query.hints]] [[mongo.query.hints]]
=== Hints == Hints
Index hints can be applied in two ways, using the index name or its field definition. Index hints can be applied in two ways, using the index name or its field definition.
@ -21,7 +21,7 @@ template.query(Person.class)
==== ====
[[mongo.query.cursor-size]] [[mongo.query.cursor-size]]
=== Cursor Batch Size == Cursor Batch Size
The cursor batch size defines the number of documents to return in each response batch. The cursor batch size defines the number of documents to return in each response batch.
==== ====
@ -33,7 +33,7 @@ Query query = query(where("firstname").is("luke"))
==== ====
[[mongo.query.collation]] [[mongo.query.collation]]
=== Collations == Collations
Using collations with collection operations is a matter of specifying a `Collation` instance in your query or operation options, as the following two examples show: Using collations with collection operations is a matter of specifying a `Collation` instance in your query or operation options, as the following two examples show:
@ -50,7 +50,7 @@ List<Person> results = template.find(query, Person.class);
==== ====
[[mongo.query.read-preference]] [[mongo.query.read-preference]]
=== Read Preference == Read Preference
The `ReadPreference` to use can be set directly on the `Query` object to be run as outlined below. The `ReadPreference` to use can be set directly on the `Query` object to be run as outlined below.
@ -66,7 +66,7 @@ template.find(Person.class)
NOTE: The preference set on the `Query` instance will supersede the default `ReadPreference` of `MongoTemplate`. NOTE: The preference set on the `Query` instance will supersede the default `ReadPreference` of `MongoTemplate`.
[[mongo.query.comment]] [[mongo.query.comment]]
=== Comments == Comments
Queries can be equipped with comments which makes them easier to look up in server logs. Queries can be equipped with comments which makes them easier to look up in server logs.

Loading…
Cancel
Save