Scala Play Salat Aggregate Example

送分小仙女□ 提交于 2020-02-02 11:21:58

问题


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


回答1:


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
  )
)


来源:https://stackoverflow.com/questions/18903848/scala-play-salat-aggregate-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!