FieldsPair & Criteria

Use FieldsPair, Condition, logical grouping, id alias, and Driver-native filter to construct dynamic MongoDB conditions.


FieldsPair and Condition

java
FieldsPair.pair(field, value)
FieldsPair.pair(field, value, condition)
FieldsPair.pair(field, condition)

• eq, notEq, between, gt/gte, lt/lte

• in, notIn, like, regex, exists

• isNull, isNotNull, all, near, nearSphere, elemMatch

Dynamic conditions and autoRangePair

java
.fields(
    pair("status", "ACTIVE"),
    minAge == null ? null : pair("age", minAge, gte),
    FieldsPair.autoRangePair("createdAt", from, to)
)

from + to

between

from only

gte

to only

lte

Neither

Returns null, so it can be naturally excluded from dynamic conditions.

AND / OR / NOT grouping

java
.fields()
.and(f -> f.fields(
    pair("status", List.of("ACTIVE_1", "ACTIVE_2"), in),
    pair("age", 20, gte)
))
.or(f -> f.fields(
    pair("name", "kim", like),
    pair("name", "lee", like)
))
.notAny(f -> f.fields(pair("banned", true)))
.end()

FieldBuilder supports and, or, not, notAny, notAll, and allows direct specification of the root logical operator.

Field name and id rules

java
pair("id", value)          // _id
pair("parent.id", value)   // parent._id
pair("incidentId", value)  // incidentId
pair("_id", value)         // _id

Only the exact id path segment is normalized to _id. A valid 24-character hex String passed to the id alias can be converted to ObjectId, but a String specified directly as _id will not be automatically converted. String field paths are based on physical MongoDB field names.

Driver-native filter

java
.driverFilter(
    Filters.and(
        Filters.eq("status", "ACTIVE"),
        Filters.size("tags", 3)
    )
)
.end()
.findAll()
.execute();

driverFilter(Bson) uses the Driver definition as is, without reinterpreting raw BSON or field mapping.

© 2026 Byeolnaerim. All rights reserved.IntroductionPrivacy Policy