File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed
main/scala/com/avsystem/commons/mongo/typed
test/scala/com/avsystem/commons/mongo/typed Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -45,9 +45,16 @@ sealed trait MongoUpdate[T] {
4545 case ArrayElementsQualifier .FirstMatching () => " $"
4646 case ArrayElementsQualifier .Each () => " $[]"
4747 case ArrayElementsQualifier .Filtered (filter) =>
48- val name = s " filter ${arrayFilters.size}"
49- arrayFilters.add(Bson .document(name, filter.toBson))
50- s " $$ [ $name] "
48+ val identifier = s " filter ${arrayFilters.size}"
49+ val (name, query) = filter match {
50+ case MongoFilter .PropertyValueFilter (prop, propertyFilter) =>
51+ // https://www.mongodb.com/docs/manual/reference/operator/update/positional-filtered/#update-all-documents-that-match-arrayfilters-in-an-array
52+ identifier + MongoPropertyRef .Separator + prop.rawPath -> propertyFilter
53+ case filter =>
54+ identifier -> filter
55+ }
56+ arrayFilters.add(Bson .document(name, query.toBson))
57+ s " $$ [ $identifier] "
5158 }
5259
5360 val newPath = path + MongoPropertyRef .Separator + rawQualifier
Original file line number Diff line number Diff line change @@ -68,6 +68,10 @@ class MongoUpdateTest extends AnyFunSuite {
6868 """ {"$set": {"intList.$[filter0]": 5}}, [{"filter0": {"$lt": 0}}]""" )
6969 assert(toString(Rte .ref(_.innerList).updateAll(_.ref(_.int).set(5 ))) ==
7070 """ {"$set": {"innerList.$[].int": 5}}, []""" )
71+ assert(toString(Rte .ref(_.innerList).updateFiltered(_.ref(_.int).lt(0 ), _.ref(_.int).set(5 ))) ==
72+ """ {"$set": {"innerList.$[filter0].int": 5}}, [{"filter0.int": {"$lt": 0}}]""" )
73+ assert(toString(Rte .ref(_.innerList).updateFiltered(_.ref(_.intMap(" abc" )).lt(0 ), _.ref(_.int).set(5 ))) ==
74+ """ {"$set": {"innerList.$[filter0].int": 5}}, [{"filter0.intMap.abc": {"$lt": 0}}]""" )
7175 }
7276
7377 test(" compound updates" ) {
You can’t perform that action at this time.
0 commit comments