Skip to content

Commit

Permalink
Implement addField for Aggregate (#42)
Browse files Browse the repository at this point in the history
* Implement addField for Aggregate

* Support single and multiple arguments for addFields
  • Loading branch information
lenguyenthanh committed Jun 23, 2024
1 parent cccc4c5 commit 1b42a95
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package mongo4cats.operations
import com.mongodb.client.model.{Aggregates, BucketAutoOptions, Facet => JFacet, GraphLookupOptions, MergeOptions, UnwindOptions}
import mongo4cats.AsJava
import org.bson.conversions.Bson
import com.mongodb.client.model.Field

trait Aggregate extends AsJava {

Expand Down Expand Up @@ -223,6 +224,22 @@ trait Aggregate extends AsJava {
*/
def lookup(from: String, pipeline: Aggregate, as: String): Aggregate

/** Creates a \$addFields pipeline stage
*
* <p>With \$addFields, you can adds the new fields the document.</p>
*
* @param name
* the name of the new field
* @param value
* the value of the new field
* @return
* the \$addFields pipeline stage [[https://www.mongodb.com/docs/manual/reference/operator/aggregation/addFields/]]
* @since 3.4
*/
def addFields[TExpression](fields: List[(String, TExpression)]): Aggregate

def addFields[TExpression](fields: (String, TExpression)*): Aggregate = addFields(fields.toList)

/** Creates a graphLookup pipeline stage for the specified filter
*
* @param from
Expand Down Expand Up @@ -321,7 +338,10 @@ object Aggregate {
def out(databaseName: String, collectionName: String): Aggregate = empty.out(databaseName, collectionName)
def merge(collectionName: String, options: MergeOptions = new MergeOptions()): Aggregate = empty.merge(collectionName, options)
def replaceWith[TExpression](value: TExpression): Aggregate = empty.replaceWith(value)
def lookup(from: String, pipeline: Aggregate, as: String): Aggregate = empty.lookup(from, pipeline, as)
def addFields[TExpression](fields: (String, TExpression)*): Aggregate = empty.addFields(fields.toList)
def addFields[TExpression](fields: List[(String, TExpression)]): Aggregate = empty.addFields(fields)

def lookup(from: String, pipeline: Aggregate, as: String): Aggregate = empty.lookup(from, pipeline, as)

def graphLookup[TExpression](
from: String,
Expand Down Expand Up @@ -384,6 +404,12 @@ final private case class AggregateBuilder(

def replaceWith[TExpression](value: TExpression): Aggregate = AggregateBuilder(Aggregates.replaceWith(value) :: aggregates)

def addFields[TExpression](fields: List[(String, TExpression)]): Aggregate = {
val jFields: List[Field[?]] = fields.map { case (name, value) => new Field(name, value) }
AggregateBuilder(Aggregates.addFields(asJava(jFields)) :: aggregates)

}

def lookup(from: String, pipeline: Aggregate, as: String): Aggregate =
AggregateBuilder(Aggregates.lookup(from, pipeline.toBson, as) :: aggregates)

Expand Down

0 comments on commit 1b42a95

Please sign in to comment.