本文介绍了按日期在Morphia中进行汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将基于PostgreSQL的应用程序迁移到MongoDB。我正在使用Morphia连接到MongoDB。
I'm trying to migrate a PostgreSQL-based application to MongoDB. I'm using Morphia to connect to MongoDB.
在我的应用中,我有以下查询来获取Hibernate的测量列表:
In my app I have this query to get a list of measurements with Hibernate:
...
jpq = jpq.from(qm)
.where(qm.parameter.eq(parameter));
jpq = jpq.where(qm.dateTime.between(startDate, endDate));
...
case MINUTE:
jpq = jpq.groupBy(qm.dateTime.minute());
case HOUR:
jpq = jpq.groupBy(qm.dateTime.hour());
case DAY:
jpq = jpq.groupBy(qm.dateTime.dayOfMonth());
case MONTH:
jpq = jpq.groupBy(qm.dateTime.month());
case YEAR:
jpq = jpq.groupBy(qm.dateTime.year());
....
我正在尝试在Morphia中创建这样的查询:
I am trying to create a query like this in Morphia:
db.MongoMeasurement.aggregate(
[
{
$group : {
_id : { month: { $month: "$dateTime" }, day: { $dayOfMonth: "$dateTime" }, year: { $year: "$dateTime" } },
averageValue: { $avg: "$value" },
count: { $sum: 1 }
}
}
]
)
..但是我很难找到有关如何在Morphia中编写此查询的文档。
.. but I'm having trouble finding documentation on how to write this query in Morphia.
推荐答案
我没有找到如何在Morphia中编写查询的方法,但是我使用Jongo解决了我的问题
I dont found how write the query in Morphia, but i solve my problem using Jongo
....
DB db = mongo.getDB("BD");
Jongo jongo = new Jongo(db);
jongoCo = jongo.getCollection("MongoMeasurement");
....
List<MongoMeasurement> cursor = jongoCo.aggregate("{$group:{"
+ "_id:{ month: { $month: '$dateTime' }, day: { $dayOfMonth: '$dateTime' }, year: { $year: '$dateTime' } },"
+ "value: { $avg: '$value' },"
+ "dateTime:{$min:'$dateTime'},"
+ "count:{$sum:1}"
+ "}}")
.as(MongoMeasurement.class);
这篇关于按日期在Morphia中进行汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!