From 7f06bf81b309ccc142ab59354363966ac7fad770 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Tue, 2 Jul 2024 14:57:31 +0700 Subject: [PATCH 1/2] Add expr operator for Filter https://www.mongodb.com/docs/manual/reference/operator/query/expr/ --- .../src/main/scala/mongo4cats/operations/Filter.scala | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 From bba2d8a06aa058149328e4add64c475cbd711b82 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Tue, 2 Jul 2024 15:04:20 +0700 Subject: [PATCH 2/2] Add test for Filter.expr --- .../src/test/scala/mongo4cats/operations/FilterSpec.scala | 4 ++++ 1 file changed, 4 insertions(+) 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') }