首页 文章

Scala Play Salat Aggregate示例

提问于
浏览
2

我在后端使用Scala Play 2.x和MongoDB,我必须承认Salat对mongo CRUD操作有很好的支持 .

但到目前为止,我没有找到任何关于如何使用SALAT调用mongo聚合函数的好例子,如$ unwind,$ match,$ group或aggregate pipeline .

例如

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

UPDATE (ALTERNATIVE) 我没有找到任何帮助系统地解释SALAT中聚合查询的用法 . 所以作为一个解决方法,我还添加了 casbah ,其中有一个
支持SBT中的AGGREGATE QUERIES,并能够与SALAT并行开展工作 .

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

提前致谢

1 回答

  • 1

    我的萨拉特版本:

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

相关问题