8 changed files with 236 additions and 126 deletions
@ -1,8 +1,20 @@
@@ -1,8 +1,20 @@
|
||||
[[mongo.query.count]] |
||||
= Counting Documents |
||||
|
||||
The template API offers various methods to count the number of documents matching a given criteria. |
||||
One of them outlined below. |
||||
|
||||
==== |
||||
[source,java] |
||||
---- |
||||
template.query(Person.class) |
||||
.matching(query(where("firstname").is("luke"))) |
||||
.count(); |
||||
---- |
||||
==== |
||||
|
||||
In pre-3.x versions of SpringData MongoDB the count operation used MongoDBs internal collection statistics. |
||||
With the introduction of xref:reference/client-session-transactions.adoc#mongo.transactions[MongoDB Transactions] this was no longer possible because statistics would not correctly reflect potential changes during a transaction requiring an aggregation-based count approach. |
||||
With the introduction of xref:mongodb/client-session-transactions.adoc#mongo.transactions[MongoDB Transactions] this was no longer possible because statistics would not correctly reflect potential changes during a transaction requiring an aggregation-based count approach. |
||||
So in version 2.x `MongoOperations.count()` would use the collection statistics if no transaction was in progress, and the aggregation variant if so. |
||||
|
||||
As of Spring Data MongoDB 3.x any `count` operation uses regardless the existence of filter criteria the aggregation-based count approach via MongoDBs `countDocuments`. |
||||
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
[[mongo.query.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 |
||||
directly there are several methods for those options. |
||||
|
||||
=== Hints |
||||
|
||||
Index hints can be applied in two ways, using the index name or its field definition. |
||||
|
||||
==== |
||||
[source,java] |
||||
---- |
||||
template.query(Person.class) |
||||
.matching(query("...").withHint("index-to-use")); |
||||
|
||||
template.query(Person.class) |
||||
.matching(query("...").withHint("{ firstname : 1 }")); |
||||
---- |
||||
==== |
||||
|
||||
=== Cursor Batch Size |
||||
|
||||
The cursor batch size defines the number of documents to return in each response batch. |
||||
==== |
||||
[source,java] |
||||
---- |
||||
Query query = query(where("firstname").is("luke")) |
||||
.cursorBatchSize(100) |
||||
---- |
||||
==== |
||||
|
||||
=== 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: |
||||
|
||||
==== |
||||
[source,java] |
||||
---- |
||||
Collation collation = Collation.of("de"); |
||||
|
||||
Query query = new Query(Criteria.where("firstName").is("Amél")) |
||||
.collation(collation); |
||||
|
||||
List<Person> results = template.find(query, Person.class); |
||||
---- |
||||
==== |
||||
|
||||
=== Read Preference |
||||
|
||||
The `ReadPreference` to use can be set directly on the `Query` object to be run as outlined below. |
||||
|
||||
==== |
||||
[source,java] |
||||
---- |
||||
template.find(Person.class) |
||||
.matching(query(where(...)).withReadPreference(ReadPreference.secondary())) |
||||
.all(); |
||||
---- |
||||
==== |
||||
|
||||
NOTE: The preference set on the `Query` instance will supersede the default `ReadPreference` of `MongoTemplate`. |
||||
|
||||
=== Comments |
||||
|
||||
Queries can be equipped with comments which makes them easier to look up in server logs. |
||||
|
||||
==== |
||||
[source,java] |
||||
---- |
||||
template.find(Person.class) |
||||
.matching(query(where(...)).comment("Use the force luke!")) |
||||
.all(); |
||||
---- |
||||
==== |
||||
|
||||
Loading…
Reference in new issue