Frage

I am using Scala Play 2.x with MongoDB in backend and I must confess that Salat has wonderful support for mongo CRUD operations.

But so far I didn't find any good example of how I can call mongo aggregate function using SALAT like $unwind, $match, $group or aggregate pipeline.

For example

db.posts.aggregate([
 {
   $unwind :"$tag"
 },
 { $group :
          {
             _id :"$tags",
              count : {$sum :1}
          }
}, 
{
   $sort : {$post :-1}    
 },
{
   $limit :1
 }
])

UPDATE (ALTERNATIVE) I didn't find any help which systematically explain usage of aggregate query in SALAT. So as an work around I also added casbah which has a
support for AGGREGATE QUERIES in SBT and able to open work in parallel with SALAT.

val appDependencies = Seq(
"se.radley" %% "play-plugins-salat" % "1.3.0",
"org.mongodb" %% "casbah" % "2.6.3"
 )

Thanks in advance

War es hilfreich?

Lösung

My salat version:

libraryDependencies ++= Seq(
  "se.radley" %% "play-plugins-salat" % "1.4.0"
)

The code example:

dao.collection.aggregate(
  MongoDBObject(
    "$unwind" -> "$tag"
  ),
  MongoDBObject(
    "$group" -> MongoDBObject(
      "_id" -> "$tags",
      "count" -> MongoDBObject("$sum" -> 1)
    )
  ),
  MongoDBObject(
    "$sort" -> MongoDBObject(
      "$post" -> -1
    )
  ),
  MongoDBObject(
    "$limit" -> 1
  )
)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top