mongodb分组聚合查询以及spring-data-mongodb实现

1. mongodb复杂查询

查找2020年1月1日以后有操作记录但是2020年6月1日后未上线的用户

db.user_history.aggregate([
    {
        $match : {
			'updateTime' : { $gte : ISODate("2020-01-01T00:00:00Z") }
        }
    },
    {
        $group: {
            _id: '$userId',
            last: { $last: '$updateTime'},
        }
    },
    {
        $project: {
            _id: 1,
            last: 1
        }
    },
    {
        $match : {
            'last': { $lt: ISODate("2020-06-01T00:00:00Z") }
        }
    }
]);

2. spring-data-mongodb实现

// match
MatchOperation match = match(where("updateTime").gte(beginTime));
// group
GroupOperation group = group("userId").last("updateTime").as("last");
// project
ProjectionOperation project = project("last");
// match2
MatchOperation match2 = match(where("last").lt(endTime));
AggregationResults<UserResult> results = mongoTemplate.aggregate(
		newAggregation(match, group, project, match2), UserHistory.class, UserResult.class);
return results.getMappedResults();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值