表结构及云函数代码如下所示,我想只对ops_card的update_time进行dateToString操作,请问该如何写呢?
ops_card 集合
{
......
"expiration_date":{
"$date":"2021-09-22T00:00:00Z"
},
"update_time":{
"$date":"2021-09-22T01:43:35.293Z"
},
}
ops_goods 集合
{
......
"status":1,
"update_time":{
"$date":"2021-09-22T01:26:04.576Z"
}
}
云函数代码
let lssue_list = await db.collection('ops_card').aggregate().sort({
status: 1,
update_time: -1
}).match({
_id: _.in(id_array)
}).lookup({
from: 'ops_goods',
localField: 'goods_id',
foreignField: '_id',
as: 'goodsList',
}).replaceRoot({
newRoot: $.mergeObjects([$.arrayElemAt(['$goodsList', 0]), '$$ROOT'])
}).project({
update_time_show: $.dateToString({
date: '$update_time',
format: '%Y-%m-%d %H:%M:%S'
})
}).limit(limit_num).skip(skip_num).end();
在需要处理的集合后使用project对字段进行dateToString操作,代码如下所示:
let lssue_list = await db.collection('ops_card').aggregate().sort({ status: 1, update_time: -1 }).match({ _id: _.in(id_array) }).project({ goods_id: 1, card_number: 1, card_password: 1, goods_number: 1, card_url: 1, qrcode_url: 1, status: 1, update_time_show: $.dateToString({ date: '$update_time', format: '%Y-%m-%d %H:%M:%S', timezone: 'Asia/Shanghai' }), expiration_date_show: $.dateToString({ date: '$expiration_date', format: '%Y-%m-%d', timezone: 'Asia/Shanghai' }) }).lookup({ from: 'ops_goods', localField: 'goods_id', foreignField: '_id', as: 'goodsList', }).replaceRoot({ newRoot: $.mergeObjects([$.arrayElemAt(['$goodsList', 0]), '$$ROOT']) }).project({ goodsList: 0, Issuer_openid: 0, goods_explain: 0, goods_id: 0, goods_url: 0, specs: 0 }).limit(limit_num).skip(skip_num).end();