使用spring-data-mongo库可以按时间间隔分组的解决方法如下:
org.springframework.boot
spring-boot-starter-data-mongodb
@Document(collection = "your_collection_name")
public class YourEntity {
@Id
private String id;
private Date timestamp;
// getters and setters
}
public interface YourRepository extends MongoRepository {
List findByTimestampBetween(Date start, Date end);
}
findByTimestampBetween
方法来按时间间隔查询数据:@Autowired
private YourRepository yourRepository;
public List getDataByTimeInterval(Date start, Date end) {
return yourRepository.findByTimestampBetween(start, end);
}
这样就可以按时间间隔分组查询MongoDB中的数据了。