Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement addField for Aggregate #42

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,20 @@ 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
def addFields[TExpression](name: String, value: TExpression): Aggregate

/** Creates a graphLookup pipeline stage for the specified filter
*
* @param from
Expand Down Expand Up @@ -321,6 +336,7 @@ 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 addFields[TExpression](name: String, value: TExpression): Aggregate = empty.addFields(name, value)
def lookup(from: String, pipeline: Aggregate, as: String): Aggregate = empty.lookup(from, pipeline, as)

def graphLookup[TExpression](
Expand Down Expand Up @@ -384,6 +400,10 @@ final private case class AggregateBuilder(

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

def addFields[TExpression](name: String, value: TExpression): Aggregate = AggregateBuilder(
Aggregates.addFields(new Field(name, value)) :: aggregates
)

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

Expand Down
Loading