Skip to content

Commit

Permalink
Add expr operator for Filter (#47)
Browse files Browse the repository at this point in the history
* Add expr operator for Filter

https://www.mongodb.com/docs/manual/reference/operator/query/expr/

* Add test for Filter.expr
  • Loading branch information
lenguyenthanh authored Jul 2, 2024
1 parent 5ec6241 commit 618c2b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions modules/kernel/src/main/scala/mongo4cats/operations/Filter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down

0 comments on commit 618c2b3

Please sign in to comment.