MongoDB取一条数据中的值
MongoDB表
let table = {
_id: ‘ObjectId’,
year: ‘number’,//年份
week: ‘number’, //第几周
weekday: [
{
times: [{
start: ‘dateTime’, //开始时间
end: ‘dateTime’, //结束时间
group: [{}],
}]
}
]
};
service实现类
public List findDutyAppraiserByDate(Date date, Long id) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setFirstDayOfWeek(Calendar.MONDAY); //设置一周的第一天是周一
int week = cal.get(Calendar.WEEK_OF_YEAR);//第几周
int w=cal.get(Calendar.DAY_OF_WEEK)-1;//周几
int year = cal.get(Calendar.YEAR);//年
//获取这一周的数据
Map<String,Object> map = new HashMap<>();
map.put(“year”,year);
map.put(“week”,week);
Bson sort = Sorts.ascending(“startDate”);
FindIterable findIterable = collection.find(new Document(map));
findIterable.sort(sort);//排序
Document document = findIterable.first();//查询单条数据
List weekday = (List) document.get(“weekday”);
Document weekdayDocument = weekday.get(w-1);
List times = (List) weekdayDocument.get(“times”);
List list= new ArrayList<>();
for(int i = 0;i < times .size();i ++){
Document timesDocuments = times.get(i);
ClassesDT classesDT = new ClassesDT();
classesDT.setStartTime((LocalDate) timesDocuments.get(“start”));
classesDT.setEndTime((LocalDate) timesDocuments.get(“end”));
list.add(classesDT);
}
return list;
}