Skip to content

Commit df9c041

Browse files
committed
Added reference documentation for Sorts and Projections.
JAVA-1776, JAVA-1777
1 parent 5e97bfa commit df9c041

File tree

3 files changed

+215
-1
lines changed

3 files changed

+215
-1
lines changed

docs/reference/content/builders/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ title = "Builders"
1111

1212
The driver provides several classes that make it easier to use the CRUD API.
1313

14-
- [Filters]({{< relref "filters.md" >}}): Documentation of the driver's support for query filters
14+
- [Filters]({{< relref "filters.md" >}}): Documentation of the driver's support for building query filters
15+
- [Projections]({{< relref "projections.md" >}}): Documentation of the driver's support for building projections
16+
- [Sorts]({{< relref "sorts.md" >}}): Documentation of the driver's support for building sort criteria
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
+++
2+
date = "2015-03-19T14:27:51-04:00"
3+
title = "Projections"
4+
[menu.main]
5+
parent = "Builders"
6+
weight = 20
7+
pre = "<i class='fa'></i>"
8+
+++
9+
10+
## Filters
11+
12+
The [`Projections`]({{< apiref "com/mongodb/client/model/Projections" >}}) class provides static factory methods for all the MongoDB
13+
projection opererators. Each method returns an instance of the [`Bson`]({{< relref "bson/documents.md#bson" >}}) type, which can in turn
14+
be passed to any method that expects a projection.
15+
16+
For brevity, you may choose to import the methods of the `Projections` class statically:
17+
18+
```java
19+
import com.mongodb.client.model.Projections.*;
20+
```
21+
22+
All the examples below assume this static import.
23+
24+
### Inclusion
25+
26+
By default, all fields of each document are projected. To specify the inclusion of one or more fields (which implicitly excludes all
27+
other fields except `_id`), use the `include` method.
28+
29+
This example includes the `quantity` field and (implicitly) the `_id` field:
30+
31+
```java
32+
include("quantity")
33+
```
34+
35+
This example includes the `quantity` and `totalAmount` fields and (implicitly) the `_id` field:
36+
37+
```java
38+
include("quantity", "totalAmount")
39+
```
40+
41+
### Exclusion
42+
43+
To specify the exclusion of one or more fields (which implicitly includes all other fields), use the `exclude` method.
44+
45+
This example excludes the `quantity` field:
46+
47+
```java
48+
exclude("quantity")
49+
```
50+
51+
This example excludes the `quantity` and `totalAmount` fields:
52+
53+
```java
54+
exclude("quantity", "totalAmount")
55+
```
56+
57+
### Exclusion of _id
58+
59+
To specify the exclusion of the `_id` field, use the `excludeId` method:
60+
61+
```java
62+
excludeId()
63+
```
64+
65+
which is just shorthand for:
66+
67+
```java
68+
exclude("_id")
69+
```
70+
71+
### Array Element Match with a Supplied Filter
72+
73+
To specify a projection that includes only the first element of an array that matches a supplied query filter (the
74+
[elemMatch]({{< docsref "reference/operator/projection/elemMatch" >}}) operator), use the `elemMatch` method that takes a
75+
field name and a filter.
76+
77+
This example projects the first element of the `orders` array where the `quantity` field is greater that `3`:
78+
79+
```java
80+
elemMatch("orders", Filters.gt("quantity", 3))
81+
```
82+
83+
### Array Element Match with an Implicit Filter
84+
85+
To specify a projection that includes only the first element of an array that matches the filter supplied as part of the query (the
86+
[positional $ operator]({{< docsref "reference/operator/projection/positional/#projection" >}})), use the `elemMatch` method that takes
87+
just a field name.
88+
89+
This example projects the first element of the `orders` array that matches the query filter:
90+
91+
```java
92+
elemMatch("orders")
93+
```
94+
95+
### Slice
96+
97+
To project [a slice of an array]({{< docsref "reference/operator/projection/slice" >}}), use one of the `slice` methods.
98+
99+
This example projects the first `7` elements of the `tags` array:
100+
101+
```java
102+
slice("tags", 7)
103+
```
104+
105+
This example skips the first `2` elements of the `tags` array and projects the next `5`:
106+
107+
```java
108+
slice("tags", 2, 5)
109+
```
110+
111+
### Text Score
112+
113+
To specify a projection of [the score of a `$text` query]({{< docsref "reference/operator/query/text/#return-the-text-search-score" >}}),
114+
use the `metaTextScore` method to specify the name of the projected field.
115+
116+
This example projects the text score as the value of the `score` field:
117+
118+
```java
119+
metaTextScore("score")
120+
```
121+
122+
123+
### Combining Projections
124+
125+
To combine multiple projections, use the `fields` method.
126+
127+
This example includes the `quantity` and `totalAmount` fields and excludes the `_id` field:
128+
129+
```java
130+
fields(include("quantity", "totalAmount"), excludeId())
131+
```
132+
133+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
+++
2+
date = "2015-03-19T14:27:51-04:00"
3+
title = "Sort Criteria "
4+
[menu.main]
5+
parent = "Builders"
6+
weight = 30
7+
pre = "<i class='fa'></i>"
8+
+++
9+
10+
## Sorts
11+
12+
The [`Sorts`]({{< apiref "com/mongodb/client/model/Sorts" >}}) class provides static factory methods for all the MongoDB sort criteria
13+
operators. Each method returns an instance of the [`Bson`]({{< relref "bson/documents.md#bson" >}}) type, which can in turn be passed to
14+
any method that expects sort criteria.
15+
16+
For brevity, you may choose to import the methods of the `Sorts` class statically:
17+
18+
```java
19+
import com.mongodb.client.model.Sorts.*;
20+
```
21+
22+
All the examples below assume this static import.
23+
24+
### Ascending
25+
26+
To specify an ascending sort, use one of the `ascending` methods.
27+
28+
This example specifies an ascending sort on the `quantity` field:
29+
30+
```java
31+
ascending("quantity")
32+
```
33+
34+
This example specifies an ascending sort on the `quantity` field, followed by an ascending sort on the `totalAmount` field:
35+
36+
```java
37+
ascending("quantity", "totalAmount")
38+
```
39+
40+
### Descending
41+
42+
To specify a descending sort, use one of the `descending` methods.
43+
44+
This example specifies a descending sort on the `quantity` field:
45+
46+
```java
47+
descending("quantity")
48+
```
49+
50+
This example specifies a descending sort on the `quantity` field, followed by a descending sort on the `totalAmount` field:
51+
52+
53+
```java
54+
descending("quantity", "totalAmount")
55+
```
56+
57+
### Text Score
58+
59+
To specify a sort by [the score of a `$text` query]({{< docsref "reference/operator/query/text/#sort-by-text-search-score" >}}), use the
60+
`metaTextScore` method to specify the name of the projected field.
61+
62+
This example specifies a sort on the score of a `$text` query that will be projected into the `scoreValue` field in a projection on the
63+
same query:
64+
65+
```java
66+
metaTextScore("scoreValue")
67+
```
68+
69+
### Combining sort criteria
70+
71+
To specify the combination of multiple sort criteria, use the `orderBy` method.
72+
73+
This example specifies an ascending sort on the `quantity` field, followed by an ascending sort on the `totalAmount` field, followed by a
74+
descending sort on the `orderDate` field:
75+
76+
```java
77+
orderBy(ascending("quantity", "totalAmount"), descending("orderDate"))
78+
```
79+

0 commit comments

Comments
 (0)