本文介绍了Scala Play Salat聚合示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在后端将MongoDB与Scala Play 2.x配合使用,我必须承认Salat对mongo CRUD操作提供了出色的支持.

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

但是到目前为止,我还没有找到如何使用SALAT调用mongo聚合函数的好例子,例如$ unwind,$ match,$ group或聚合管道.

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.

例如

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

更新(替代),我没有找到任何系统地解释使用方法的帮助 SALAT中的汇总查询.因此,作为解决方案,我还添加了 casbah 支持SBT中的AGGREGATE QUERIES,并可以与SALAT并行打开工作.

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

预先感谢

推荐答案

我的礼拜版本:

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

代码示例:

dao.collection.aggregate(
  MongoDBObject(
    "$unwind" -> "$tag"
  ),
  MongoDBObject(
    "$group" -> MongoDBObject(
      "_id" -> "$tags",
      "count" -> MongoDBObject("$sum" -> 1)
    )
  ),
  MongoDBObject(
    "$sort" -> MongoDBObject(
      "$post" -> -1
    )
  ),
  MongoDBObject(
    "$limit" -> 1
  )
)

这篇关于Scala Play Salat聚合示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 13:43