Ниже запрос на агрегацию базы данных mongo для данных о весне, предназначенный для получения доступных учителей в зависимости от того, у кого нет рабочей нагрузки 20 часов / 15 часов в неделю.
public List<Calendar> findAvailableTeachersV2(List<java.util.Date> leanerPlannedSlotList, int hoursPerWeek) {
log.info("TeachersAvailabilityQuery.getTeachers()");
Aggregation aggregation = Aggregation.newAggregation(
match(where("schedule.startDate").in(leanerPlannedSlotList)
.andOperator(where("schedule.status").is("AVAILABLE"))),
Aggregation.unwind("$schedule"),
project("_id", "timeZone", "schedule").and("schedule.startDate").as("startDate")
.and(DateOperators.IsoWeek.isoWeek("schedule.startDate")).as("week").and("schedule.status")
.as("status").and(LiteralOperators.Literal.asLiteral(0.5)).as("duration"),
// TODO
group("_id", "week", "status")
.addToSet(ConditionalOperators.when(Criteria.where("status").is("AVAILABLE"))
.thenValueOf("startDate").otherwise(""))
.as("startDate").push("duration").as("duration").count().as("bookedDuration"),
// TODO
// Need to check intersection
project("_id").and(SetOperators.SetIntersection.arrayAsSet("startDate").intersects("startDate"))
.as("availableSlots")
.and(ConditionalOperators.when(Criteria.where("schedule.status").is("BOOKED"))
.thenValueOf("bookedDuration").otherwise(0))
.as("bookedDuration"),
Aggregation.unwind("availableSlots", true),
// TODO
// Need to check the sum of bookedDuration
group("_id").addToSet("availableSlots").as("availableSlots").sum("bookedDuration")
.as("bookedDuration"),
// TODO
// Need to check size condition for: availableSlots
match(where("bookedDuration").gt(hoursPerWeek).andOperator(where("availableSlots").size(0).gt(0))),
Aggregation.unwind("availableSlots"), group("_id").addToSet("availableSlots").as("availableSlots"));
AggregationResults<Calendar> results = mongoOperations.aggregate(aggregation, COLLECTION_NAME, Calendar.class);
List<Calendar> calendarList = results.getMappedResults();
return calendarList;
}
Пожалуйста, обратитесь к приведенной ниже трассировке стека исключений:
org.springframework.data.mongodb.UncategorizedMongoDbException:Command failed with error 16006: 'can't convert from BSON type string to Date' on server cluster0-shard-00-00-uwrbw.mongodb.net:27017. The full response is { "operationTime" : { "$timestamp" : { "t" : 1553603169, "i" : 5 } }, "ok" : 0.0, "errmsg" : "can't convert from BSON type string to Date", "code" : 16006, "codeName" : "Location16006", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1553603169, "i" : 5 } }, "signature" : { "hash" : { "$binary" : "gkjiKThKxdvt+K/4stNTlMjM9rE=", "$type" : "00" }, "keyId" : { "$numberLong" : "6616309008233922561" } } } }; nested exception is com.mongodb.MongoCommandException: Command failed with error 16006: 'can't convert from BSON type string to Date' on server cluster0-shard-00-00-uwrbw.mongodb.net:27017. The full response is { "operationTime" : { "$timestamp" : { "t" : 1553603169, "i" : 5 } }, "ok" : 0.0, "errmsg" : "can't convert from BSON type string to Date", "code" : 16006, "codeName" : "Location16006", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1553603169, "i" : 5 } }, "signature" : { "hash" : { "$binary" : "gkjiKThKxdvt+K/4stNTlMjM9rE=", "$type" : "00" }, "keyId" : { "$numberLong" : "6616309008233922561" } } } }
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:131)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2589)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:499)
at org.springframework.data.mongodb.core.MongoTemplate.executeCommand(MongoTemplate.java:437)
at org.springframework.data.mongodb.core.MongoTemplate$BatchAggregationLoader.aggregateBatched(MongoTemplate.java:3137)
at org.springframework.data.mongodb.core.MongoTemplate$BatchAggregationLoader.aggregate(MongoTemplate.java:3113)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1937)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1841)