Match & Filter
Filter documents using the $match stage - MongoDB's equivalent of SQL's WHERE clause.
Basic Equality
konduct.collection<Product>()
.match {
Product::status eq "active"
Product::category eq "Electronics"
}
.toList()
Multiple conditions are combined with AND by default.
Comparison Operators
Greater Than / Less Than
.match {
Product::price gt 100 // Greater than
Product::price gte 100 // Greater than or equal
Product::stock lt 10 // Less than
Product::rating lte 3.5 // Less than or equal
}
Not Equal
Set Operators
In / Not In
.match {
Product::category `in` listOf("Electronics", "Books", "Toys")
Product::status nin listOf("deleted", "archived")
}
String Operations
Regex
.match {
Product::name regex "iPhone.*"
Product::description regex "wireless".toRegex(RegexOption.IGNORE_CASE)
}
Null Checks
Logical Operators
Implicit AND
OR
NOT
Expression-Based Matching
For complex comparisons involving multiple fields or calculations:
konduct.collection()
.match {
Order::status eq "active"
expr {
(Order::quantity * Order::price) gte 1000
}
}
.toList()
Comparing Fields
Complex Calculations
Using Variables (in Lookup Pipelines)
.lookup {
from(OrderItem::class)
let {
"orderId" to Order::id
"minQuantity" to 5
}
pipeline {
match {
expr {
and(
OrderItem::orderId eq variable("orderId"),
OrderItem::quantity gte variable("minQuantity")
)
}
}
}
into("items")
}
Real-World Examples
Find High-Value Orders
fun findHighValueOrders(): List<Order> {
return konduct.collection<Order>()
.match {
Order::status eq "completed"
Order::total gte 1000
Order::orderDate gte thirtyDaysAgo()
}
.sort { Order::total.desc() }
.toList()
}
Search Products
fun searchProducts(query: String, minPrice: Double, maxPrice: Double): List<Product> {
return konduct.collection<Product>()
.match {
Product::name regex query.toRegex(RegexOption.IGNORE_CASE)
Product::status eq "active"
Product::price gte minPrice
Product::price lte maxPrice
}
.sort { Product::rating.desc() }
.toList()
}
Find Expiring Subscriptions
fun findExpiringSubscriptions(days: Int): List<Subscription> {
val today = Date()
val futureDate = Date(today.time + days * 24 * 60 * 60 * 1000)
return konduct.collection<Subscription>()
.match {
Subscription::status eq "active"
Subscription::expiryDate gte today
Subscription::expiryDate lte futureDate
Subscription::autoRenew eq false
}
.sort { Subscription::expiryDate.asc() }
.toList()
}
String-Based Fallback
For dynamic field names:
Performance Tips
-
Index matched fields:
-
Filter early:
-
Use selective filters:
See Also
- Sorting - Order results
- Grouping - Aggregate data
- API Reference - Complete match API