我使用SpringBoot(v1.5.10),SpringData和MongoDb;
db.login.aggregate(
[
{
"$match" : {
"user._id" : NumberLong(204)
}
},
{
"$group" : {
"_id" : {
"datetime" : { $dateToString: { format: "%d-%m-%Y", date: "$datetime"}},
"ip" : "$ip",
},
count: { $sum: 1 }
}
}
]);
2011-01-16算上16
Criteria criteria = Criteria.where("user._id").is(paramuserid).andOperator(
Criteria.where("datetime").gte(paramfirstdate),
Criteria.where("datetime").lt(paramseconddate)
);
AggregationOperation match = Aggregation.match(criteria);
List<AggregationOperation> aggregationoperations = new ArrayList <AggregationOperation>();
aggregationoperations.add(match);
aggregationoperations.add(Aggregation.group("datetime"));
AggregationResults<?> aggregationresults = this.mngT.aggregate(Aggregation.newAggregation(aggregationoperations), Login.class, Object.class);
2011-01-10 09:00:01算1
2011-01-10 09:15:00算上1
Mongo聚合组日期时间没有“时间”,我尝试类似的条件查询不工作,因为组有时间部分。如何在criteriaquery
中使用“$ dateToString”答案 0 :(得分:1)
我解决了。
Criteria criteria = Criteria.where("user._id").is(paramuserid).andOperator(
Criteria.where("datetime").gte(paramfirstdate),
Criteria.where("datetime").lt(paramseconddate)
);
AggregationOperation match = Aggregation.match(criteria);
List<AggregationOperation> aggregationoperations = new ArrayList <AggregationOperation>();
aggregationoperations.add(match);
aggregationoperations.add(Aggregation.project("ip","datetime").andExpression("datetime").dateAsFormattedString("%d-%m-%Y").as("formateddate"));
aggregationoperations.add(Aggregation.group("ip","formateddate").count().as("TOT"));
AggregationResults<?> aggregationresults = this.mngT.aggregate(Aggregation.newAggregation(aggregationoperations), Login.class, Object.class);