木四
木四
  • 发布:2022-11-20 11:39
  • 更新:2022-11-21 11:35
  • 阅读:265

如何将时间戳转为日期

分类:uniCloud

1.数据库数据如下
[{"_id":"63798c84f1492c0001845296","type":0,"lunar":0,"name":"test","time":1668873600000,"user_id":"6379890a51251300010b25c8"},{"_id":"63798c84f1492c0001845297","type":1,"lunar":0,"name":"test","time":1668873600000,"user_id":"6379890a51251300010b25c8"}]

2.准备将time字段的时间戳转为日期对象以便后续执行日期转换操作

  1. 代码如下
    const $ = db.command.aggregate
    const {
    result: { errCode, data },
    } = await db
    .collection('special-days')
    .aggregate()
    .match({
    user_id: db.getCloudEnv('$cloudEnv_uid'),
    })
    .addFields({
    day: $.add([new Date(0), '$time']),
    })
    .sort({
    day: -1,
    })
    .end()

  2. 报如下错误
    InternalServerError errCode: InternalServerError | errMsg: Command failed with error 40180 (Location40180): 'Invalid $addFields :: caused by :: an empty object is not a valid value. Found empty object at path day' on server 192.168.146.109:3717. The full response is { "operationTime" : { "$timestamp" : { "t" : 1668915057, "i" : 3 } }, "ok" : 0.0, "errmsg" : "Invalid $addFields :: caused by :: an empty object is not a valid value. Found empty object at path day", "code" : 40180, "codeName" : "Location40180", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1668915057, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "/pjU/YnD6uto8c3I wwntakBt2U=", "$type" : "00" }, "keyId" : { "$numberLong" : "7124964235662065714" } } } }
    VM6200 WAService.js:1 Error: errCode: InternalServerError | errMsg: Command failed with error 40180 (Location40180): 'Invalid $addFields :: caused by :: an empty object is not a valid value. Found empty object at path day' on server 192.168.146.109:3717. The full response is { "operationTime" : { "$timestamp" : { "t" : 1668915057, "i" : 3 } }, "ok" : 0.0, "errmsg" : "Invalid $addFields :: caused by :: an empty object is not a valid value. Found empty object at path day", "code" : 40180, "codeName" : "Location40180", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1668915057, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "/pjU/YnD6uto8c3I wwntakBt2U=", "$type" : "00" }, "keyId" : { "$numberLong" : "7124964235662065714" } } } }

2022-11-20 11:39 负责人:无 分享
已邀请:
DCloud_uniCloud_CRL

DCloud_uniCloud_CRL

直接按照time字段排序就好了,不需要 addFields

  • 木四 (作者)

    现在的问题是使用addFields报错;你看到的示例不是我真实的需求,我真时的需求是:$timestamp字段记录的是生日;取$timestamp中的month与date来计算离下个的生日还有多少天remainDay,再根据remainDay从小到大排列

    2022-11-21 14:15

  • 木四 (作者)

    所以我首先得将timestamp转成date,再用date对应的方法取出month与date

    2022-11-21 14:18

  • DCloud_uniCloud_CRL

    回复 w***@163.com: time字段是有空值吗?完整查询语句贴上看下。

    2022-11-21 14:32

要回复问题请先登录注册