diff --git a/modules/kernel/src/main/scala/mongo4cats/operations/Filter.scala b/modules/kernel/src/main/scala/mongo4cats/operations/Filter.scala index 0f23c190..af7a7a1a 100644 --- a/modules/kernel/src/main/scala/mongo4cats/operations/Filter.scala +++ b/modules/kernel/src/main/scala/mongo4cats/operations/Filter.scala @@ -321,6 +321,17 @@ object Filter extends AsJava { def where(javaScriptExpression: String): Filter = FilterBuilder(Filters.where(javaScriptExpression)) + /** Creates a filter that matches all documents for which the given expression is true. + * + * @param expression + * the aggregation expression + * @return + * the filter + * @since 3.6 + */ + def expr[TExpression](expr: TExpression): Filter = + FilterBuilder(Filters.expr(expr)) + /** Creates a filter that matches all documents where the value of a field is an array that contains all the specified values. * * @param fieldName diff --git a/modules/kernel/src/test/scala/mongo4cats/operations/FilterSpec.scala b/modules/kernel/src/test/scala/mongo4cats/operations/FilterSpec.scala index 00e08220..fd03031d 100644 --- a/modules/kernel/src/test/scala/mongo4cats/operations/FilterSpec.scala +++ b/modules/kernel/src/test/scala/mongo4cats/operations/FilterSpec.scala @@ -81,6 +81,10 @@ class FilterSpec extends AnyWordSpec with Matchers { Filter.where("foo") isTheSameAs Filters.where("foo") } + "expr" in { + Filter.expr("$expr: { $gt: [ $spent , $budget ] }") isTheSameAs Filters.expr("$expr: { $gt: [ $spent , $budget ] }") + } + "all" in { Filter.all("foo", "bar") isTheSameAs Filters.all("foo", 'b', 'a', 'r') }