Mongodb 将时间戳转换为年月日日期

使用dateToString 方法进行转换 并且通过format指定转换日期格式

        Integer userId=aaa;
        GroupOperation groupOperation = Aggregation.group("day").sum("money").as("todayIncome").count().as("todayPayCount");
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("userId").is(userId)),
                project("userId","money").andExpression("{$dateToString: {date: { $add: {'$createTime', [0]} }, format: '%Y%m%d'}}", new Date(28800000)).as("day"),
                groupOperation,
                sort(Sort.Direction.ASC, "_id")
        );

注意:

1.必须使用 $dateToString: {date: { $add: 通过求和进行date数据转换 如果去掉后面的会报解析错误

org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 16006 (Location16006): 'can't convert from BSON type long to Date' on server localhost:50000. The full response is {"ok": 0.0, "errmsg": "can't convert from BSON type long to Date", "code": 16006, "codeName": "Location16006"}; nested exception is com.mongodb.MongoCommandException: Command failed with error 16006 (Location16006): 'can't convert from BSON type long to Date' on server localhost:50000. The full response is {"ok": 0.0, "errmsg": "can't convert from BSON type long to Date", "code": 16006, "codeName": "Location16006"}

2.必须增加 new Date(28800000) 的时间 原因是增加了8个时区的偏移量

参考链接

MongoDB中的日期查询的坑
用过 mongodb 吧, 这三个大坑踩过吗?

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值