将Mongo查询转换为spring Mongooperations
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将Mongo查询转换为spring Mongooperations相关的知识,希望对你有一定的参考价值。
有人可以帮助我将这个原生mongo查询转换为spring数据吗?它使用setIntersection的聚合函数。
任何人都可以将查询转换为Mongodb for Java(qazxsw poi)?
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'
])
?
如果您尝试将聚合管道转换为Java代码,setIntersection
将使用MongoDB Compass选项为您执行此操作。这是你的代码
export to language
以上是关于将Mongo查询转换为spring Mongooperations的主要内容,如果未能解决你的问题,请参考以下文章
将查询 mongo 转换为 spring Mongooperations
将 $facet mongo 查询转换为 Spring Data