Java 多个字段上的Mongodb聚合。如何将别名_id字段映射到适当的对象
下面是使用morphia的mongo db查询和相应的Java代码。 当我使用多个字段聚合时,我要在其中别名_id字段。这个别名将帮助我在查询结果和Java对象之间创建正确的映射 查询:Java 多个字段上的Mongodb聚合。如何将别名_id字段映射到适当的对象,java,mongodb,aggregation-framework,morphia,Java,Mongodb,Aggregation Framework,Morphia,下面是使用morphia的mongo db查询和相应的Java代码。 当我使用多个字段聚合时,我要在其中别名_id字段。这个别名将帮助我在查询结果和Java对象之间创建正确的映射 查询: db.ulog.aggregate([ { "$match": { "$and": [ { "cdate": { "$gte": 1550008600000 } }, { "c
db.ulog.aggregate([
{
"$match": {
"$and": [
{
"cdate": {
"$gte": 1550008600000
}
},
{
"cdate": {
"$lt": 1554056999999
}
}
]
}
},
{
"$group": {
"_id": {"channelUserCode":"$pucode","channelActivityCode":"$pacode"},
"point": {
"$sum": "$point"
},
"count": {
"$sum": 1
}
}
},
{
"$project": {
"_id": 1,
"point": 1,
"count": 1,
}
}
])
结果:
{
"_id" : {
"channelUserCode" : NumberLong(709098775838588289),
"channelActivityCode" : NumberLong(-1525000763901071495)
},
"point" : NumberLong(15),
"count" : 5.0
}
Java代码:
AdvancedDatastore datastore = (AdvancedDatastore) userLogDao.getDatastore();
String collectionName = CalUtils.getUserLogCollectionName(startDate);
Query<UserLog> query = datastore.createQuery(collectionName, UserLog.class);
query.project(UserLog.ID, true).project(UserLog.CHANNEL_ACTIVITY_CODE,true).
project(UserLog.CHANNEL_ACTIVITY_PLATFORM_CODE,true).
project(UserLog.POINT,true).project(UserLog.CHANNEL_USER_CODE, true).
project(UserLog.USER_ID, true);
query.field(UserLog.CDATE).greaterThanOrEq(startDate);
query.field(UserLog.CDATE).lessThan(endDate);
AggregationPipeline pipeline = datastore.createAggregation(collectionName, UserLog.class)
.match(query)
.group( id(grouping(UserLog.CHANNEL_USER_CODE), grouping(UserLog.CHANNEL_ACTIVITY_CODE)),
grouping("points", sum(UserLog.POINT)),
grouping("count", new Accumulator("$sum", 1)))
.project(Projection.projection("_id"), Projection.projection("points"), Projection.projection("count"));
//pipeline.aggregate(channelActivityMap);
AdvancedDatastore datastore=(AdvancedDatastore)userLogDao.getDatastore();
String collectionName=CalUtils.getUserLogCollectionName(startDate);
Query Query=datastore.createQuery(collectionName,UserLog.class);
project(UserLog.ID,true).project(UserLog.CHANNEL\u ACTIVITY\u CODE,true)。
项目(UserLog.CHANNEL\u ACTIVITY\u PLATFORM\u CODE,true)。
项目(UserLog.POINT,true)。
项目(UserLog.USER\u ID,true);
field(UserLog.CDATE).greaterThanOrEq(startDate);
字段(UserLog.CDATE).lessThan(endDate);
AggregationPipeline=datastore.createAggregation(collectionName,UserLog.class)
.match(查询)
.group(id(分组(UserLog.CHANNEL\u用户代码)、分组(UserLog.CHANNEL\u活动代码)),
分组(“点”,总和(UserLog.POINT)),
分组(“计数”,新累加器($sum),1)))
.project(Projection.Projection(“id”)、Projection.Projection(“点”)、Projection.Projection(“计数”);
//管道集料(渠道活动图);