有人可以帮助我将本机mongo查询转换为spring数据吗?它使用带有setIntersection的聚合函数。
任何人都可以将该查询转换为Mongo for Java(org.springframework.data.mongodb.core.MongoOperations
)吗?
db.TeachersCalendar.aggregate([
{
$match : {
$and: [
{"schedule.startDate": {$in:[ISODate("2020-11-15T23:30:00Z"), ISODate("2019-11-24T01:00:00Z"), ISODate("2020-07-26T23:00:00Z")]}},
{"schedule.status": 'AVAILABLE'}
]
}
},
{
$unwind: '$schedule'
},
{
$project: {
_id: 1,
status: '$schedule.status',
startDate: '$schedule.startDate',
week: {
$concat: [
{
$toString: {
$isoWeek: '$schedule.startDate'
}
},
'-',
{
$toString: {
$isoWeekYear: '$schedule.startDate'
}
}
]
},
duration: {$literal: 0.5}
}
},
{
$group: {
_id: {
id: '$_id',
week: '$week',
status: '$status'
},
startDate: {
$addToSet: {
$cond: [
{
$eq: ['$status', 'AVAILABLE']
},
'$startDate',
null
]
}
},
bookedDuration: {
$sum: '$duration'
}
}
},
{
$project: {
_id: 1,
availableSlots: {
$setIntersection: [[ISODate("2020-11-15T23:30:00Z"), ISODate("2019-11-24T01:00:00Z"), ISODate("2020-07-26T23:00:00Z")], '$startDate']
},
bookedDuration: {
$cond: [{$eq: ['$_id.status', 'BOOKED']}, '$bookedDuration', 0]
}
}
},
{
$unwind: { path: '$availableSlots', preserveNullAndEmptyArrays: true}
},
{
$group: {
_id: {
id: '$_id.id',
week: '$_id.week'
},
availableSlots: {$addToSet: '$availableSlots'},
bookedDuration: {$sum : '$bookedDuration'}
}
},
{
$match:{
$and :[
{
bookedDuration: { $lt: 20 }
},
{
availableSlots: {$not: {$size: 0}}
}
]
}
},
{
$unwind: '$availableSlots'
},
{
$group: {
_id: '$_id.id',
availableSlots: {
$addToSet: '$availableSlots'
}
}
}
])
如何处理必须与日期列表相交的春季数据上的setIntersection
?
答案 0 :(得分:0)
如果您尝试将聚合管道转换为Java代码,MongoDB Compass将使用export to language选项为您完成。这是您的代码
import java.util.Arrays;
import org.bson.Document;
import org.bson.BsonNull;
Arrays.asList(new Document("$match",
new Document("$and", Arrays.asList(new Document("schedule.startDate",
new Document("$in", Arrays.asList(new java.util.Date(1605483000000L),
new java.util.Date(1574557200000L),
new java.util.Date(1595804400000L)))),
new Document("schedule.status", "AVAILABLE")))),
new Document("$unwind", "$schedule"),
new Document("$project",
new Document("_id", 1L)
.append("status", "$schedule.status")
.append("startDate", "$schedule.startDate")
.append("week",
new Document("$concat", Arrays.asList(new Document("$toString",
new Document("$isoWeek", "$schedule.startDate")), "-",
new Document("$toString",
new Document("$isoWeekYear", "$schedule.startDate")))))
.append("duration",
new Document("$literal", 0.5d))),
new Document("$group",
new Document("_id",
new Document("id", "$_id")
.append("week", "$week")
.append("status", "$status"))
.append("startDate",
new Document("$addToSet",
new Document("$cond", Arrays.asList(new Document("$eq", Arrays.asList("$status", "AVAILABLE")), "$startDate",
new BsonNull()))))
.append("bookedDuration",
new Document("$sum", "$duration"))),
new Document("$project",
new Document("_id", 1L)
.append("availableSlots",
new Document("$setIntersection", Arrays.asList(Arrays.asList(new java.util.Date(1605483000000L),
new java.util.Date(1574557200000L),
new java.util.Date(1595804400000L)), "$startDate")))
.append("bookedDuration",
new Document("$cond", Arrays.asList(new Document("$eq", Arrays.asList("$_id.status", "BOOKED")), "$bookedDuration", 0L)))),
new Document("$unwind",
new Document("path", "$availableSlots")
.append("preserveNullAndEmptyArrays", true)),
new Document("$group",
new Document("_id",
new Document("id", "$_id.id")
.append("week", "$_id.week"))
.append("availableSlots",
new Document("$addToSet", "$availableSlots"))
.append("bookedDuration",
new Document("$sum", "$bookedDuration"))),
new Document("$match",
new Document("$and", Arrays.asList(new Document("bookedDuration",
new Document("$lt", 20L)),
new Document("availableSlots",
new Document("$not",
new Document("$size", 0L)))))),
new Document("$unwind", "$availableSlots"),
new Document("$group",
new Document("_id", "$_id.id")
.append("availableSlots",
new Document("$addToSet", "$availableSlots"))))