I need help refactoring this code to AggregationOperation as the Document code is hard to maintain
Aggregates.match(new Document("created_by", userId)),
Aggregates.addFields(
new Field<>("created_at_date",
new Document("$dateToString",
new Document("format", "%Y-%m-%d")
.append("date", "$created_at")
)
)
),
Aggregates.group("$created_at_date"),
Aggregates.addFields(
new Field<>("isoDate",
new Document("$toDate", "$_id")
)
),
Aggregates.sort(Sorts.descending("isoDate")),
Aggregates.group(null, Accumulators.push("dates", "$isoDate")),
Aggregates.project(Projections.fields(
Projections.computed("continuousCount",
new Document("$reduce",
new Document("input",
new Document("$range",
Arrays.asList(1, new Document("$size", "$dates")))
)
.append("initialValue",
new Document("count", 1)
.append("stopCounting", false)
.append("previousDate", new Document("$arrayElemAt", Arrays.asList("$dates", 0)))
)
.append("in", new Document("$cond", Arrays.asList(
new Document("$or", Arrays.asList(
"$$value.stopCounting",
new Document("$ne", Arrays.asList(
new Document("$subtract",
Arrays.asList(
new Document("$arrayElemAt",
Arrays.asList("$dates", "$$this")),
-86400000)),
"$$value.previousDate")
)
)
),
new Document("count", "$$value.count")
.append("stopCounting", true)
.append("previousDate", "$$value.previousDate"),
new Document("count",
new Document("$add",
Arrays.asList("$$value.count", 1)))
.append("stopCounting", false)
.append("previousDate",
new Document("$arrayElemAt",
Arrays.asList("$dates", "$$this")))
)))
))
)),
Aggregates.project(new Document("_id", 0).append("count", "$continuousCount.count"))