0
# Query Building & Filters
1
2
Comprehensive query building utilities for creating MongoDB queries, filters, sorts, projections, and updates with type safety and fluent APIs.
3
4
## Capabilities
5
6
### Filters Utility Class
7
8
Factory methods for creating query filters with type-safe field matching.
9
10
```java { .api }
11
public final class Filters {
12
// Equality operations
13
public static Bson eq(String fieldName, Object value);
14
public static Bson ne(String fieldName, Object value);
15
16
// Comparison operations
17
public static Bson gt(String fieldName, Object value);
18
public static Bson gte(String fieldName, Object value);
19
public static Bson lt(String fieldName, Object value);
20
public static Bson lte(String fieldName, Object value);
21
22
// Array operations
23
public static Bson in(String fieldName, Object... values);
24
public static Bson nin(String fieldName, Object... values);
25
26
// Logical operations
27
public static Bson and(Bson... filters);
28
public static Bson or(Bson... filters);
29
public static Bson not(Bson filter);
30
public static Bson nor(Bson... filters);
31
32
// Pattern matching
33
public static Bson regex(String fieldName, String pattern);
34
public static Bson regex(String fieldName, Pattern pattern);
35
36
// Field existence
37
public static Bson exists(String fieldName);
38
public static Bson exists(String fieldName, boolean exists);
39
40
// Type checking
41
public static Bson type(String fieldName, BsonType type);
42
public static Bson type(String fieldName, String type);
43
}
44
```
45
46
### Updates Utility Class
47
48
Factory methods for creating update operations with type safety.
49
50
```java { .api }
51
public final class Updates {
52
// Field updates
53
public static Bson set(String fieldName, Object value);
54
public static Bson unset(String fieldName);
55
public static Bson rename(String fieldName, String newFieldName);
56
public static Bson setOnInsert(String fieldName, Object value);
57
58
// Numeric updates
59
public static Bson inc(String fieldName, Number amount);
60
public static Bson mul(String fieldName, Number amount);
61
public static Bson min(String fieldName, Object value);
62
public static Bson max(String fieldName, Object value);
63
64
// Array updates
65
public static Bson push(String fieldName, Object value);
66
public static Bson pushEach(String fieldName, List<?> values);
67
public static Bson pull(String fieldName, Object value);
68
public static Bson pullAll(String fieldName, List<?> values);
69
public static Bson addToSet(String fieldName, Object value);
70
public static Bson popFirst(String fieldName);
71
public static Bson popLast(String fieldName);
72
73
// Combined updates
74
public static Bson combine(Bson... updates);
75
}
76
```
77
78
### Projections Utility Class
79
80
Factory methods for creating field projections.
81
82
```java { .api }
83
public final class Projections {
84
public static Bson include(String... fieldNames);
85
public static Bson exclude(String... fieldNames);
86
public static Bson excludeId();
87
public static Bson elemMatch(String fieldName, Bson filter);
88
public static Bson slice(String fieldName, int limit);
89
public static Bson slice(String fieldName, int skip, int limit);
90
public static Bson textScore(String fieldName);
91
public static Bson meta(String fieldName, String metaFieldName);
92
public static Bson fields(Bson... projections);
93
}
94
```
95
96
### Sorts Utility Class
97
98
Factory methods for creating sort specifications.
99
100
```java { .api }
101
public final class Sorts {
102
public static Bson ascending(String... fieldNames);
103
public static Bson descending(String... fieldNames);
104
public static Bson orderBy(Bson... sorts);
105
public static Bson textScore(String fieldName);
106
public static Bson metaTextScore();
107
}
108
```
109
110
**Usage Examples:**
111
112
```java
113
import com.mongodb.client.model.Filters;
114
import com.mongodb.client.model.Updates;
115
import com.mongodb.client.model.Projections;
116
import com.mongodb.client.model.Sorts;
117
118
// Complex query with filters
119
FindIterable<Document> results = collection.find(
120
Filters.and(
121
Filters.gte("age", 18),
122
Filters.lt("age", 65),
123
Filters.in("status", "active", "pending"),
124
Filters.regex("name", "^A.*", "i")
125
)
126
);
127
128
// Update operations
129
UpdateResult result = collection.updateMany(
130
Filters.eq("department", "Engineering"),
131
Updates.combine(
132
Updates.inc("salary", 5000),
133
Updates.set("lastUpdated", new Date()),
134
Updates.addToSet("skills", "MongoDB")
135
)
136
);
137
138
// Projection and sorting
139
FindIterable<Document> projected = collection.find()
140
.projection(Projections.fields(
141
Projections.include("name", "email", "department"),
142
Projections.excludeId()
143
))
144
.sort(Sorts.orderBy(
145
Sorts.ascending("department"),
146
Sorts.descending("salary")
147
));
148
```