scala_mongodb驱动的问题

scala mongodb groupby 多个字段

今天同事问groupby多个字段是怎么写的

然后上$group (aggregation)
看了下,发现要写表达式,看了这篇文章MongoDB aggregate 运用篇 个人总结,发现要像如下写:

1
2
3
4
5
6
7
8

多列group,根据name和status进行多列

db.collection.aggregate([

{$group:{_id:{name:"$name",st:"$status"},count:{$sum:1}}}

]);

那么代码中应该怎么写呢,研究了半天,看到这篇文章MongoDB getCollection with a specific document class

发现我们得从定义mongodbClient开始定义个codeRegistry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

case class GroupByCode(name:String,code:String) {
// val id: String = _id.toString
}
private[bigdata] val personCodecProvider = Macros.createCodecProvider[GroupByCode]()
private[bigdata] val codecRegistry = fromRegistries( fromProviders(personCodecProvider), DEFAULT_CODEC_REGISTRY )

private[bigdata] val connectionString = new ConnectionString(utils.dbUrl)
private[bigdata] val options = MongoClientSettings
.builder()
.codecRegistry(codecRegistry)
.clusterSettings(ClusterSettings.builder().applyConnectionString(connectionString).build())
.connectionPoolSettings(ConnectionPoolSettings.builder().applyConnectionString(connectionString).build())
.serverSettings(ServerSettings.builder().build()).credentialList(connectionString.getCredentialList)
.sslSettings(SslSettings.builder().applyConnectionString(connectionString).build())
.socketSettings(SocketSettings.builder().applyConnectionString(connectionString).build())
.build()
//private[bigdata] val mongoClient = MongoClient(utils.dbUrl)
private[bigdata] val mongoClient = MongoClient(options)

然后在用的地方直接

1
2
3
val groupBy = group(GroupByCode("$userType","$code"),sum("count", 1))

Await.result(prdb.aggregate(filter(Filters.and(Filters.eq("code", parkCode), Filters.gte("exitDate", date), Filters.lt("exitDate", c.getTime))) :: groupBy :: Nil).toFuture(), read_timeout)

当然mongodb-scala-driver得升级到2.0版才支持caseClassCodec

New in 2.0, the Scala driver allows you to use case classes to represent documents in a collection via the Macros helper. Simple case classes and nested case classes are supported. Hierarchical modelling can be achieve by using a sealed trait and having case classes implement the parent trait.

欢迎关注我的公众号:沉迷Spring
显示 Gitment 评论
0%