FieldsPair & Criteria
تكوين شروط MongoDB الديناميكية باستخدام FieldsPair و Condition و التجميع المنطقي و id alias و Driver-native filter.
FieldsPair و Condition
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
شروط ديناميكية و autoRangePair
.fields(
pair("status", "ACTIVE"),
minAge == null ? null : pair("age", minAge, gte),
FieldsPair.autoRangePair("createdAt", from, to)
)من + إلى
between
من فقط
gte
إلى فقط
lte
لا شيء من الاثنين
يتم استبعاد null بشكل طبيعي من الشروط الديناميكية.
تجميع AND / OR / NOT
.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 يدعم and, or, not, notAny, notAll ويمكن أيضًا تحديد الجذر المنطقي مباشرة.
قواعد اسم الحقل و id
pair("id", value) // _id
pair("parent.id", value) // parent._id
pair("incidentId", value) // incidentId
pair("_id", value) // _idيتم تطبيع جزء مسار id الدقيق فقط إلى _id. إذا قمت بتمرير سلسلة hex صالحة مكونة من 24 حرفًا إلى alias id، يمكن تحويلها إلى ObjectId، لكن السلسلة المحددة لـ _id لا يتم تحويلها تلقائيًا. مسار حقل السلسلة يعتمد على اسم الحقل الفعلي في MongoDB.
Driver-native filter
.driverFilter(
Filters.and(
Filters.eq("status", "ACTIVE"),
Filters.size("tags", 3)
)
)
.end()
.findAll()
.execute();driverFilter(Bson) يستخدم تعريف السائق كما هو دون إعادة تفسير BSON الخام أو تخطيط الحقول.