Skip to content

Commit ba77159

Browse files
committed
Projection Helper Functions added
1 parent bcdb55d commit ba77159

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package com.sfxcode.nosql.mongo
22

3+
import org.bson.conversions.Bson
4+
import org.mongodb.scala.model.Aggregates.sort
5+
import org.mongodb.scala.model.Projections.computed
6+
37
object Aggregate extends Aggregate
48

5-
trait Aggregate extends Field with Filter with Sort
9+
trait Aggregate extends Field with Filter with Sort {
10+
11+
def compositeProjection(resultFieldName: String, keys: List[String]): Bson =
12+
computed(resultFieldName,
13+
Map[String, Any]("$concat" -> keys.map(key => Map[String, Any]("$substr" -> List("$" + key, 0, 99999)))))
14+
15+
def divideProjection(resultFieldName: String, dividendFieldName: String, divisorFieldName: String): Bson =
16+
computed(resultFieldName, Map[String, Any]("$divide" -> List("$" + dividendFieldName, "$" + divisorFieldName)))
17+
18+
def multiplyProjection(resultFieldName: String, productFieldNames: List[String]): Bson =
19+
computed(resultFieldName, Map[String, Any]("$multiply" -> productFieldNames.map(fieldname => "$" + fieldname)))
20+
21+
def sortStage(key: String, sortAscending: Boolean = true): Bson =
22+
sort(sortByKey(key, sortAscending))
23+
24+
}

0 commit comments

Comments
 (0)