Last active
November 3, 2020 09:11
-
-
Save akiatoji/8edc3d827eaceaec5bc8b3d76422e7e6 to your computer and use it in GitHub Desktop.
How to do MongoDB Group aggregation with Scala MongoDB driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.mongodb.scala.bson.collection.mutable.Document | |
import org.mongodb.scala.{MongoClient, MongoCollection, MongoDatabase} | |
import org.mongodb.scala.model.Aggregates._ | |
import org.mongodb.scala.model.Accumulators._ | |
import com.profuzio.MongoHelper._ | |
val mongoClient: MongoClient = MongoClient.apply("mongodb://localhost") | |
val database: MongoDatabase = mongoClient.getDatabase("kiwanda") | |
val collection: MongoCollection[Document] = database.getCollection("labor") | |
val v = collection.aggregate(Seq( | |
group( | |
// This is supposed to be a TExpression. Not sure what it is but this works | |
Document( "rvp" -> "$rvp", "rm" -> "$rm", "am" -> "$am" ), | |
sum("totalhours", "$TotalHours")) | |
)).results() | |
println(v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment