Search code examples
mongodbscalamongodb-querymongo-scala-driver

Scala MongoDB groupby multiple fields


I'm trying to use the following (example) query in the MongoDB Scala driver:

db.getCollection('datatype').aggregate(
[
    { "$match": { "allowedApplications": ".." }},
    { "$group": { "_id": {"name": "$name", "applicationId": "$applicationId"}, "version": { "$max": "$version" }}}
]
   )

Basically what I'm trying to translate is mainly:

"$group": { "_id": {"name": "$name", "applicationId": "$applicationId"}}

But couldn't get it to work.

I tried multiple stuff without any success.

Using Scala 2.13.1 and mongo-scala-driver 4.1.0. Any help would be appreciated.


Solution

  • The following seems to work:

    group(
            Document("name" -> "$name", "applicationId" -> "$applicationId"),
            ...
    )
    

    I'll mark this answer as correct if this works properly.